Forum Moderators: open
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).
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>)
<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.
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?
"...no problem - JavaScript works everywhere"
<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?"
Shawn
"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
The type attribute is a required element in HTML 4 and above.
Run your code through [validator.w3.org...] to check it out.
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....
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
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?
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
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?