Change href file but not query string with jQuery

I want users with JavaScript to be allowed to see the results.php page. Users with JavaScript disabled should see results_d.php.

To do this, I initially show my links with results_d.php. Then, using JavaScript to change the destination in links, only users with JavaScript enabled will see rich content.

Here are some questions about using jQuery to change the entire href destination, but how can I just change the file name and save the query string as is?

I thought something like this, but it does not work ...

$(document).ready(function()
{
    $('a').attr('href').replace('results_d', 'results');
});
+3
source share
4 answers
$(document).ready(function() { 
    $('a').attr('href', function(index, href) {
        return 'results?'+(href.split('?')[1])
    });
});
+2
$(document).ready(function() { 
  $('a').each(function() {
    $(this).attr('href',$(this).attr('href').replace('results_d', 'results'));
  });
});
0

. , . .

, :

$('a').each(function()
{ 
    this.href = this.href.replace('results_d', 'results');
});
0

jQuery(function(){
  
  jQuery("a").each(function(e,i){
    
    jQuery(this).attr("href",i.href.replace("results_d","results"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a href="/search/results_d.php?kwds=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a> 
<a href="/search/results_d.php?asd=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a> 
<a href="/search/results_d.php?kwdfgds=whatever&amp;search_mode=abc&amp;countries[]=AL‌​L" class="medtxt">View all results</a>
Hide result

I know this is a little late, but I was not in stackoverflow in 2010, and I could not help but answer this unanswered question.

Thank!!

0
source

Source: https://habr.com/ru/post/1735056/


All Articles