Closed Thread Icon

Topic awaiting preservation: a href=javascript:........... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8478" title="Pages that link to Topic awaiting preservation: a href=javascript:........... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: a href=javascript:........... <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 01-20-2003 03:55

I may be going about this all wrong but I'm playing around with some Javascript and the effect I want to have is a hyperlink that when clicked changes the bgColor...

I've tried this: <a href=javascript:document.bgColor="blue">Blue Background</a>

It does change the bgColor to blue and then it immediately spits a white page with the words Blue Background at the top.

Why does this happen?

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 01-20-2003 04:23

I think I figured it out....

I did this: <a href="#" onClick="javascript:document.bgColor='blue'">Blue Background</a>

Is there a better way to do this?

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 01-20-2003 04:26

That works just fine. You can also call a function.

code:
<html>
<head>
<title>Untitled</title>
<script type="text/javascript" language="JavaScript">
function bg(color) {
document.bgColor=color
}

</script>
</head>

<body>
<a href="javascript:bg('blue');">Blue Background</a>


</body>
</html>



. . : slicePuzzle

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 01-20-2003 05:09

ooooh I like that....

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-20-2003 05:28

This:

<a href="#" onClick="javascript:document.bgColor='blue'">Blue Background</a>

is incorrect code. The "javascript:" format should *only* be used where a URL is expected. Javascript code is expected in onclick events, not a URL, so don't use it there. (Secondly, using # for the href isn't a great idea; leave the href blank, and have the onclick event return false.)

The reason your first bit of code didn't work is because the javascript: url executes the code, and takes the return value of the last statement (in your case, 'blue'), and creates a new page with that as the HTML. to keep this from happening, void the return value:

<a href="javascript:void(document.bgColor='blue')">Blue Background</a>

Oh, and please use quotes around your HTML attributes =)

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 01-20-2003 05:30

Cool, Slime, I didn't know you could use the void function like that.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 01-20-2003 06:36

Tell me....is this incorrect too? If so what is wrong with it?

code:
<html>
<head>
<title>ozone.shtml</title>
<script type="text/javascript" language="JavaScript">
function bg(color) {
document.bgColor=color
}
</script>
<style type="text/css">
.ff9900 {position:absolute; height:25px; width:9px; left:0px; top:0px;}
.ff6600 {position:absolute; height:25px; width:9px; left:9px; top:0px;}
.993300 {position:absolute; height:25px; width:9px; left:18px; top:0px;}

</style>
</head>


<body bgcolor="black">
<div class="FF9900"><a href="javascript:void(document.bgColor='#FF9900')" onMouseover="javascript:void(document.bgColor='#FF9900')" onMouseout="javascript:void(document.bgColor='#000000')"><img src="FF9900.gif" border="0"></a></div>
<div class="FF6600"><a href="javascript:void(document.bgColor='#FF6600')" onMouseover="javascript:void(document.bgColor='#FF6600')" onMouseout="javascript:void(document.bgColor='#000000')"><img src="FF6600.gif" border="0"></a></div>
<div class="993300"><a href="javascript:void(document.bgColor='#993300')" onMouseover="javascript:void(document.bgColor='#993300')" onMouseout="javascript:void(document.bgColor='#000000')"><img src="993300.gif" border="0"></a></div>


</body>
</html>



[This message has been edited by Boudga (edited 01-20-2003).]

[This message has been edited by Boudga (edited 01-20-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-20-2003 06:55

Boudga: the onmouseover and onclick events should *not* begin with "javascript:"; they should be pure javascript code (no prefix), and return false ("return false;") in order to keep the hyperlink from taking the user anywhere. The "javascript:" and the "void(stuff)" trick are only for the href="" attribute.

[edit: i wrote that before you edited your post.]

Bugs: actually, it's an operator. I just use parenthesis for clarity. But in theory it can be used with just a space:

void 0;

[This message has been edited by Slime (edited 01-20-2003).]

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 01-20-2003 07:03

Like this?

code:
onMouseout="document.bgColor='#000000';" "return false;"



[This message has been edited by Boudga (edited 01-20-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-20-2003 16:31

Not quite; remember, there's never more than one set of quotes for a single HTML attribute. Put it all inside the same set of quotes like this:

onMouseout="document.bgColor='#000000'; return false;"

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-20-2003 17:02

Why would a mouseout need a false return?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-20-2003 18:41

Oh, good point. The return false should only be necessary for onclick or maybe for onmouseup.

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 01-20-2003 19:33

I tried adding "return false;" to Boudga's original code and got errors. That is why I went to calling the function and was so pleased when I saw the void method. Did you guys get the "return false;" to work?

readonlynet
Obsessive-Compulsive (I) Inmate

From: Connecticut
Insane since: Jan 2003

posted posted 01-21-2003 21:02

wow, never saw void used like that. Awesome, that clears things up in my head

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-22-2003 03:24

Umm.. Actualy, the javascript:viod(code...) example is in Netscape's Javascript 1.3 Reference. I thought everyone had that, it's a free PDF download.

What I'd like to know is where you'd want to use it other than a situation like this...

« BackwardsOnwards »

Show Forum Drop Down Menu