I have a script as below
$('.button1').click(function() {
document.location.href=$(this).attr('id');
});
the button1 has variable unique ids. on click, the page must redirect to url "www.example.com/index.php?id=buttonid
" but now the page is redirecting only to "button id
".
I want to add the string "www.example.com/index.php?id=
" before the current url. How can I make this possible?
You need to specify the domain:
Why not just change the second line to
First of all using
window.location
is better as according to specificationdocument.location
value was read-only and might cause you headaches in older/different browsers. Check notes @MDC DOM document.location pageAnd for the second - using
attr
jQuery method to get id is a bad practice - you should use direct native DOM accessorthis.id
as the value assigned tothis
is normal DOM element.you can get the current url with
window.location.href
but I think you will need the jQuery query plugin to manipulate the query string: http://plugins.jquery.com/project/query-object