Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: open

Message Too Old, No Replies

Putting javascript <script> statement in body

Will it work?

   
6:01 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Administrator robert_charlton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I'm dealing with some framed pages hosted on a remote server where I don't have access to the head section of the page... only to the body section (inputed via a form).

Can I put a javascript <script> statement in the body, and will it work?

The script, activated by clicking a graphic, opens a pop-up window (with content from yet another remote server).

7:23 pm on Apr 6, 2003 (gmt 0)

10+ Year Member



no problem - JavaScript works everywhere
7:48 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I don't think you will be able to do what you are looking to do because any scripts in the body will execute as they are read.

On the other hand, I know you can call certain js actions right from a link, but I don't think you could do anything advanced.(ie. <a href="blah" onClick"javascript: history.back()">Back</a>)

9:23 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The question did say about clicking on an image, so you put the image in a link that goes nowhere.

<a href="#" title="some text here" [javascript code here]><img src="filename.png" alt="some text here"></a>

or

<a href="http://othersite.com/otherpage.htm" target=_blank title="some text here"><img src="filename.png" alt="some text here"></a>

I might not have quite the right syntax in this quick email.

9:31 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Administrator robert_charlton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I don't know much about javascript, so I can't say whether this is advanced.

The link is in the form
<a href="" onclick="FunctName();return false;"><img src="clickhere.gif" alt'"pqr" width="50" height="35"></a>

...and the code is of the form:

<script language="JavaScript">
<!--
function FunctName()
{
popupWin = window.open('http*//www.remotedomain.com/xyz.asp?id=12345', 'Content','location=yes,scrollbars=yes,width=x,height=z');
window.name = 'whatever';
}
// -->
</script>

Just out of curiosity, why do you put the image into a link that goes nowhere?

9:40 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may work like this, Robert. Okay, it does, I just tested it.

<a href="" onclick="window.open('http*//www.remotedomain.com/xyz.asp?id=12345', 'Content','location=yes,scrollbars=yes,width=x,height=z'); "><img src="clickhere.gif" alt'"pqr" width="50" height="35"></a>

10:23 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"...no problem - JavaScript works everywhere"

On occation I have problems with old browsers when just plonking Javascript anywhere. For example,


<frameset ....>
<script language="JavaScript">
<!--
document.write('<frame...
etc
//-->
</script>

will work on most new browsers, but not on Netscape 4.7, whereas

<script language="JavaScript">
<!--
document.write('<frameset ....>');
document.write('<frame...
etc
//-->
</script>

works across a wider range of browsers including Netscape 4.7. (Unless I was doing something else wrong...). If you really don't want to put the function in an external file, then I'd define it in the <head> section. Other than that, I don't see why your code (post # 5) shouldn't work (although Birdman's code in post # 6 will work too).

"...why do you put the image into a link that goes nowhere?"

i.e. Why is it better to use href="#" rather than href=""? - So that you get the cursor to change to a hand to indicate it is a link when you hover over it on some browsers.

Shawn

10:54 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Don't forget to add the type="text/javascript" attribute to every <script> tag.
11:18 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Don't forget to add the type="text/javascript" attribute"

g1smd, is specifying the mime type required, or does it become redundant once the language has been specified? (Incidentally, looking back at the code I had the frameset problem with in NN4.7, I did have the type specified as you recommended)

Sorry if this is a very newbie question, and is a bit off a tangent from the main question

Shawn

11:26 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The language attribute on <script> tags is for older browsers.

The type attribute is a required element in HTML 4 and above.

Run your code through [validator.w3.org...] to check it out.

11:40 pm on Apr 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks
3:41 am on Apr 7, 2003 (gmt 0)

WebmasterWorld Administrator robert_charlton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



It may work like this, Robert. Okay, it does, I just tested it.

<a href="" onclick="window.open('http*//www.remotedomain.com/xyz.asp?id=12345', 'Content','location=yes,scrollbars=yes,width=x,height=z'); "><img src="clickhere.gif" alt'"pqr" width="50" height="35"></a>

Thanks... What's still not clear to me, though (I am a javascript newbie), is where to put the <script> tag and function statement...

If you really don't want to put the function in an external file, then I'd define it in the <head> section.

The function is in an external js file on my other pages, with a src= in the <script> tag in the head. On this page, again, I don't have access to the head. Will it work in the body with the <script> tag and function statement just preceding the link?

Hope I'm not missing something obvious....

4:04 am on Apr 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Robert

If you don't have access to the head section, you can put it in the body. (By 'it' I mean whatever javascript you want, either the function definition, or the reference to the external javascript file). All I was saying is that putting it in the body may not give the desired results with very old browsers, but with modern browsers I don't see a problem.

When bird wrote

"...any scripts in the body will execute as they are read..."
I don't think that will be a problem, as that is exactly what you want. i.e. for the code which defines the function to execute, so that after it has executed, the function will be defined. The function won't have been called, and it won't run until you call it.

With the other way of doing it (<a href="" onclick="window.open... ) you don't need to define the function, so you don't need any <script> tag.

If you have tried any of these options, and not had success, what was the behaviour or error message?

Shawn

4:46 am on Apr 7, 2003 (gmt 0)

WebmasterWorld Administrator robert_charlton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Thanks... It's all beginning to gel.

With the other way of doing it (<a href="" onclick="window.open... ) you don't need to define the function, so you don't need any <script> tag.

I'm curious why you don't have to specify script language by this method. Other than the economy of code in "the other way of doing it," is there a reason to prefer one method over another?

6:39 am on Apr 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the <a> tag you can define an attribute language="..." to specify vbscript vs javascript, but that is IE specific, not part of the HTML 4 standard. If you omit that attribute, javascript is the default. (Note, that is a different attribute to the lang="...", which is in the standard, and used to specify English vs Japanese vs Hebrew...)

I guess the advantages/disadvantages are pretty much the same for any language where you have the option of declaring and calling a function vs just writing the code in: Modularity, readability, re-useability vs the coding effort of abstracting what you are doing into a useful re-usable function.

Well, that's my understanding, but others may have more advanced knowledge...

Shawn

11:12 am on Apr 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I meant to write <a href="" onClick="javascript: window.open(...

Apparently, It will work without it though. Also, I stand corrected about functions not being to run from the body. Just tested it and it works. I could have sworn I was taught not to define functions in the body.

7:23 pm on Apr 7, 2003 (gmt 0)

10+ Year Member



On this subject,

Does anyone have any suggestions as to how to implement
<a href="" onClick="javascript: window.open('anotherpage.html','','featurelist')">
so that it works in non-script enabled browser?

I would like to something like this to be workable-
<a href="anotherpage.html" target="_blank" onClick="javascript: window.open('anotherpage.html','','featurelist')">

But this opens up 2 windows with JS browser , 1 window on a no JS browser.

I don't really wont to go down the <NOSCRIPT> root for maintainance reasons.

Is there an elegant/any solution to write a HREF tag with an onclick event so that it works on a non-script enabled browser?

7:39 pm on Apr 7, 2003 (gmt 0)

10+ Year Member



For non-javascript-enabled browsers, try:

<a href="anotherpage.html" target="_blank" onClick="javascript: window.open('anotherpage.html','','featurelist');return false;">

You need the return false to avoid 2 windows being opened by a JS-enabled browser.

7:43 pm on Apr 7, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Add return false; in the onclick statement. That will cause the link to go nowhere... Like this:

<a href="anotherpage.html" target="_blank" onClick="javascript:window.open('anotherpage.html','','featurelist');return false;">

Gaah! Too slow :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month