FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript, writing code in the address bar without clearing

 


Dougie1
Whenever I type something in the address bar of the page to try and get it to appear on the page it doesn't work. It just brings up a new blank page.

WHY!

I have tried this:

Code:

javascript:document.getElementById("ratingMessage").innerHTML="Hello";


On youtube and it doesn't work! Why? It flashes in the rating message bit for about a second until it refreshes to a new page with "Hello" written on it. How do I make it appear?
varun_dodla
This can be done by enclosing the statement in a function and then calling the function. Find an example below.

Code:
javascript:function temp(){document.getElementById("ratingMessage").innerHTML="Hello"}temp();


I don't know why and how this works but it does...

For a demo please paste the following in a .html file and open it in the browser and then type the above javascript in the address bar and press enter.(tested in IE please allow the script when the information bar comes up)
Code:

<html>
<head>
<title></title>
</head>
<body>
Hi How are you?
<div id="ratingMessage"></div>
</BODY>
</html>


Hope this helps.
Dougie1
That is really useful! Thanks so much Very Happy
alkutob
varun_dodla

thank you 4 your help ... it is very useful
Aredon
You can run a whole list of statements in the address bar if you separate them by semicolons.
In the same manner, the last statement will be used as a return value for the address bar.
void 0 works well.
Code:

javascript:document.getElementById("ratingMessage").innerHTML="Hello";void 0

"void" is an operator -- not a function -- like "return" so it doesn't require parenthesis.
Void accepts a parameter and doesn't do anything with it then it returns undefined.
Basically void 0 can be replaced with undefined.
Oh and void 0 is a convention btw, you could've just have easy have used void"".
Code:

javascript:document.getElementById("ratingMessage").innerHTML="Hello";undefined

You can also use an alert since alert returns undefined.
Code:

javascript:document.getElementById("ratingMessage").innerHTML="Hello";alert(alert("I return undefined"))


The reason varun_dodla's code works is because if a function is called and doesn't return a value, it automatically returns undefined.
Code:

javascript:function temp(){/*useless function*/};alert(temp())


Whatever the case, the last statement in the address bar must evaluate to undefined in some way or another lest the browser navigate away.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.