2

How can I write something ("hello my client" for example) in the browser's address bar with javascript after the page is completely loaded?

Mean writing something in address bar without entering - is it possible?

It seems we can do this job with JavaScript, if not can we do that with server side code?

Douglas
  • 32,530
  • 8
  • 68
  • 88
LostLord
  • 1,969
  • 9
  • 30
  • 31

5 Answers5

14

How?

This is possible, but only the part after the hostname:

history.pushState(null, "page 2", '/foo.html');

Try this in your javascript console, this effectively changes the current path with /foo.html. (It's a new html5 feature, and is available in recent browsers only.)

See mozilla docs: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries

Browser Compatibility

Why?

This is used to make ajax sites history/bookmark/link friendly by updating the URL as the content is updated. Currently most sites do this by only changing the hash part of the URL (e.g. Twitter with their #!.)

For instance Github uses this for their code browser: https://github.com/blog/760-the-tree-slider

WonderLand
  • 4,853
  • 6
  • 50
  • 71
Arnaud Le Blanc
  • 90,979
  • 22
  • 192
  • 188
  • Which shipping browsers currently support this? (Jan 28, 2011) I just checked IE8 (no), IE9 Platform Preview 7 (no), Firefox 3.6 (no), Chrome 8.0 (yes), Opera 11.0 (no) - I think therefore at the moment we can only say "yes it is possible" if we fully qualify that it only works in Browser X. For the record I was testing by typing this in the location bar of each browser: `javascript:alert(typeof(history.pushState));` if it isn't defined, it won't be supported by this browser. – scunliffe Jan 28 '11 at 16:53
  • 2
    Because they perceive the question is intrinsically evil, and by association, any valid answer must be as well. – Metal Jan 28 '11 at 16:56
2

Maybe its already answered @ Change the URL in the browser without loading the new page using JavaScript .

Community
  • 1
  • 1
Ravikiran
  • 1,300
  • 10
  • 15
1

You can set location.hash, but you can't replace the entire URI.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

The reason this is not possible is it presents a security violation. This is why phishers write a gif file over where they believe the address bar will be.

My question is why would you want to do this? The only reason I can think of is you want to make someone think they are at http://Iamreallyyourbank.com when they are at http://IamStealingYourMoney.com, which is why the security is in place.

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
Gregory A Beamer
  • 16,342
  • 3
  • 23
  • 29
-2

This is not possible. You cannot change the URL displayed in the browser. Not only would it be a horrible security practice, it would be a violation of trust to the people visiting your site.

Michael Irigoyen
  • 21,233
  • 17
  • 82
  • 125
  • 3
    Agreed - it would also be a security violation... if www.scammersCheatersAndLiars.com could change the address bar to www.reliableBank.com – scunliffe Jan 28 '11 at 16:10
  • 5
    Yes you can :-) (but not the hostname, obviously) – Arnaud Le Blanc Jan 28 '11 at 16:10
  • 1
    I should rephrase this as "no you can't" in current shipping browsers. There are some options for this in newer browsers (as per you link on the OP's question) however realistically at the moment this isn't possible (and I can't think of a legitimate - non-scam reason why one would want to do it) – scunliffe Jan 28 '11 at 16:46
  • it seems this job is not possible and we can only change the url with keeping hostname // but is it possible with server side code (php or asp.net)? – LostLord Jan 28 '11 at 18:33