tags:

views:

51

answers:

3

I'd like users with JavaScript enabled to see page results.php. Users with JavaScript disabled should see results_d.php.

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

There are a few questions on here about using jQuery to change the whole href destination, but how can I just change the file name and keep the query string as it is?

I was thinking something like this, but it's not working...

$(document).ready(function()
{
    $('a').attr('href').replace('results_d', 'results');
});
+2  A: 
$(document).ready(function() { 
    $('a').attr('href', function(index, href) {
        return 'results?'+(href.split('?')[1])
    });
});
harpax
href is undefined
works for me.. a problem could be, that the selector is somewhat generic. Maybe it would help if you would a class to those links, that you want to change and then call `$("a.your_class").attr(...)`
harpax
A: 
$(document).ready(function() { 
  $('a').each(function() {
    $(this).attr('href',$(this).attr('href').replace('results_d', 'results'));
  });
});
FrankBr
$(this).attr("href") is undefined
A: 

Thanks for the replied guys. I got a few errors with that logic though in my file. I've added my errors to the comments.

In the end, what did work for me was:

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