Hello, I was wondering, with javascript is it was possible to change aspects of the URL such as the stuff after the address without re-loading the page.
It is not possible to update the querystring in the location object without the page being reloaded. Any assignment of new values to the location object from JavaScript will load a new page.
Ive been having a play, and I’ve got stuck with something. I cant seem to stop the jumplinks from working, I need to disable them.
What I am doing is converting a html document with jumplinks, into a document where you can navigate through each section as a page. The html of other pages is still there, its just hidden with css ( display: none; ). Ive done it this way to help with accessibility.
The change from a long html doc to a short view by section/page only happens if you have javascipt enabled and is optional. I have tried changing the id with javascript to stop the jumping, but it doesnt work. The jump links dont seem to take any notice of javascript, only html. Is there a way round this?
You need to prevent the default action (which is to follow the link’s href attribute), using preventDefault() for good browsers and returnValue for IE. See this:
Thanks Raffles, thats great. I also found self.scrollTo(0, 0) to work, I am quite new to javascript, been working my way through “Simply Javascript” and “DHTML Utopia”. Would you suggest using preventDefault() over self.scrollTo(0, 0)? and are there any incompatabilities with self.scrollTo(0, 0)?
Using the scrollTo technique is a more complex solution.
W3C has the preventDefault() method while IE has the returnValue property.
You don’t need either though as returning false is also guaranteed to work.
Putting Occam’s razor at work here, returning false is the simplest means by which to successfully do the job.
On the flip-side, if you do use the scrollTo method, you will in 6 months time have forgotten why it’s there so you will need to either document that statement by placing a comment near it to remind you why.
Simple is best. return false and you or anyone else will be able to tell what’s happening there.
You know, I’ve been scratching be head over a similar issue. I want to take the query string off the URL without refreshing the page. Is that possible, or would that count as changing the location? I can see how it might be a security issue.
To the best of my knowledge Javascript cannot do this. jaavscript has many other known issues with redirection. It is better to use redirection within PHP or server side if possible in any case.