href with onClick javascript problem in new window

M

Mike

I am new to javascript and the DOM. I've searched google groups,
and although I saw numerous discussions about javascript and
hrefs, I did not see a solution to this problem. Thank you in
advance for any help you might offer.

GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.

I want a hyperlink instead of a button, so that the user can
decide whether to open in the same window or in a new window (or
new tab) via their browser controls.

PROBLEM: The following code works fine if I click to open in
the same window, but if I click the browser option to open in a
new window, the new window tries to open the href URL (the
onClick function does get executed, but seems to be ignored).
I've run this in IE6, NS7, and Opera.

<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/page1.pl";
}
</script>
...
<a href="noJS.htm" onClick="NewPage(); return false;">
...

I have also tried href="#", href="javascript:void(0);", href="",
but in all cases, the new window always attempts to open the href
text (in NS 7, if the href contains the "javascript:" pseudo-URL,
it does not even give the option of opening in new window). I do
realize that "javascript:" in an href is not good practice.

I also tried the following, with the same results:
function NewPage() {
window.open("/cgi-bin/page1.pl", "_top");
}

I also tried setting the href from in the javascript function,
with the same results; it works in the same window, but when
opening in new window, it tries to open the original href text,
not what it was changed to in the NewPage() function.
<script language="javascript">
function NewPage(index) {
document.anchors[index].href = "/cgi-bin/page1.pl";
}
</script>
...
<a name="x" href="noJS.htm" onClick="NewPage(index); return true;">
// index is the appropriate index for this anchor
...

Again, any help or pointers would be much appreciated.

Thanks,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top