homepage Welcome to WebmasterWorld Guest from 54.225.1.70
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Putting javascript <script> statement in body
Will it work?
Robert Charlton




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

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).

 

BjarneDM




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

no problem - JavaScript works everywhere

Birdman




msg:1478435
 7:48 pm on Apr 6, 2003 (gmt 0)

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>)

g1smd




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

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.

Robert Charlton




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

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?

Birdman




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

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>

ShawnR




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

"...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

g1smd




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

Don't forget to add the type="text/javascript" attribute to every <script> tag.

ShawnR




msg:1478441
 11:18 pm on Apr 6, 2003 (gmt 0)

"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

g1smd




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

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.

ShawnR




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

thanks

Robert Charlton




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

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....

ShawnR




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

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

Robert Charlton




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

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?

ShawnR




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

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

Birdman




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

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.

HocusPocus




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

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?

universalis




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

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.

DrDoc




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

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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved