473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

href with onClick javascript problem in new window

In my previous post, I wrote:
...
GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.
...
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).
...
<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/page1.pl";
}
</script>
...
<a href="noJS.htm" onClick="NewPage(); return false;">
...


I should have mentioned the following two things:

1. The above NewPage() function is just an example. The actual
function has to do some work to create the final target.

2. I originally tried just putting this into the href
href="javascript:NewPage();"
This worked fine for the same window, but when asking for a
new window, it tries to run NewPage() in the blank window,
which of course doesn't work.

Thanks,
Mike
Jul 23 '05 #1
5 10851
Mike wrote:
In my previous post, I wrote:
...
GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.
...
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).
...
<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/page1.pl";
}
</script>
...
<a href="noJS.htm" onClick="NewPage(); return false;">
...


I should have mentioned the following two things:

1. The above NewPage() function is just an example. The actual
function has to do some work to create the final target.

2. I originally tried just putting this into the href
href="javascript:NewPage();"
This worked fine for the same window, but when asking for a
new window, it tries to run NewPage() in the blank window,
which of course doesn't work.

Thanks,
Mike


<a href="noJS.htm" onclick="NewPage();return false;">

is the way to do it. I suggest you try it with:

<script type="text/javascript">
function NewPage() {
alert('here');
window.location.href = 'http://www.yahoo.com';
}
</script>

Once you have convinced yourself that the function is firing and
directing the browser to Yahoo!, you can begin to work on what is most
likely the real problem, which is that the "work" your script is doing
to build the URI is probably building an *incorrect* URI, or the code
itself has a syntax error. The way to confirm this is to use:

var uri = something;
uri += somethingElse;
uri += moreStuff + somethingDifferent;
alert(uri);
return;
window.location.href = uri;

Now you can closely examine the results of what you built and ensure
it is a valid URI. If you never get the alert, then the problem is a
syntax error in your code, which you can check by either
double-clicking the yellow "!" in the bottom left corner of Internet
Explorer or entering "javascript:" in the address bar of
Mozilla/Netscape/Firefox and hitting enter.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #2
Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<40***************@agricoreunited.com>...
Mike wrote:
In my previous post, I wrote:
...
GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.
...
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).
...
<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/page1.pl";
}
</script>
...
<a href="noJS.htm" onClick="NewPage(); return false;">
...

Thanks,
Mike


<a href="noJS.htm" onclick="NewPage();return false;">

is the way to do it. I suggest you try it with:

<script type="text/javascript">
function NewPage() {
alert('here');
window.location.href = 'http://www.yahoo.com';
}
</script>

Once you have convinced yourself that the function is firing and
directing the browser to Yahoo!, you can begin to work on what is most
likely the real problem, which is that the "work" your script is doing
to build the URI is probably building an *incorrect* URI, or the code
itself has a syntax error. The way to confirm this is to use:
...


Thank you very much for the speedy reply with advice and
pointers to the other info.

Perhaps I should first clarify what I mean by "clicking the browser
option to open in a new window." By this I mean, with IE or NS, I
right click on the <a> object, and select "Open Link in New Window"
from pulldown.

Do I need to detect this differently than via onClick. Do I need
to detect the right mouse, and then figure out which of the pulldown
elements they asked for?

In any case, I did try your suggestion. Here is the exact code I ran:

<html>
<head>
<script type="text/javascript">
function NewPage() {
alert('here');
window.location.href = 'http://www.yahoo.com';
}
</script>
</head>
<body>
<a href="noJS.htm" onClick="NewPage(); return false;">CLICK</a>
</body>
</html>

If I open in the same window, the alert shows up and I get
to yahoo. If I open in new window (as described above) then it
does NOT fire the function, and just tries to open noJS.htm.

I also now see that in IE, if I right click on the <a> object,
and then select "Open Link" (which should then open it in the
same window), this also DOES NOT fire the function. It seems
that only the left mouse causes onClick to occur.

Thanks again. And sorry if I am being dense.

Mike
Jul 23 '05 #3
After referring to the IE DOM reference that Grant Wagner
pointed out in his followup, I see that the onClick event
only applies to the LEFT mouse button. There is another event
for the RIGHT context menu button (which I was using to get
the new window).

The NS solution is not so immediately obvious to me, but it
must be a similar thing.

Sorry for the unnecessary last post where I asked if I need
to detect right mouse differently. I should have looked at
those DOM references before posting.

Those pointers to the DOM referencese are a big help (I left
them in the inclusion below).
Thanks!
Mike

Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<40***************@agricoreunited.com>...
Mike wrote:
In my previous post, I wrote:
...
GOAL: (very simple) Provide a hyperlink which, when clicked,
calls a javascript function which opens a new URL.
...
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).
...
<script language="javascript">
function NewPage() {
location.href = "/cgi-bin/page1.pl";
}
</script>
...
<a href="noJS.htm" onClick="NewPage(); return false;">
...


I should have mentioned the following two things:

1. The above NewPage() function is just an example. The actual
function has to do some work to create the final target.

2. I originally tried just putting this into the href
href="javascript:NewPage();"
This worked fine for the same window, but when asking for a
new window, it tries to run NewPage() in the blank window,
which of course doesn't work.
...

<a href="noJS.htm" onclick="NewPage();return false;">

is the way to do it. I suggest you try it with:

<script type="text/javascript">
function NewPage() {
alert('here');
window.location.href = 'http://www.yahoo.com';
}
</script>
...
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 23 '05 #4
Here's a (partial) solution to my original post.

ORIGINAL GOAL: Provide a hyperlink which calls a javascript
function to create a new URL, which will work whether clicked
with the left mouse, or the right mouse context menu (e.g., to
open the URL in a new tab or a new window).

SOLUTION: This requires (1) detecting the right mouse click in
addition to the left, and (2) javascript function must set the
element.href and then return "true" (which allows the context
menu to work as normal, and then if the user does elect to open
the link, it will follow the replaced href).

The following works for at least IE6, NS7.1, Mozilla 1.6, but
not Opera. I can't see how to detect the right mouse click in
Opera, so if the user right clicks to open new page or window,
they will get noJS.htm. I did not try this with a Mac.

<script type="text/javascript">
function NewPage(element) {
// create desired URI and assign to the element's href.
target = "http://www.yahoo.com"
element.href = target;
}
</script>
...
<a href="noJS.htm"
onClick= "NewPage(this); return true;"
oncontextmenu="NewPage(this); return true;"> ... </a>

Mike

ze******@comcast.net (Mike) wrote in message news:<48**************************@posting.google. com>...
After referring to the IE DOM reference that Grant Wagner
pointed out in his followup, I see that the onClick event
only applies to the LEFT mouse button. There is another event
for the RIGHT context menu button (which I was using to get
the new window).
...
Those pointers to the DOM referencese are a big help (I left
them in the inclusion below).
Thanks!
Mike

Grant Wagner <gw*****@agricoreunited.com> wrote in message news:<40***************@agricoreunited.com>...
Mike wrote:
In my previous post, I wrote:
> ...
> GOAL: (very simple) Provide a hyperlink which, when clicked,
> calls a javascript function which opens a new URL.
> ...
> 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).
> ...
> <script language="javascript">
> function NewPage() {
> location.href = "/cgi-bin/page1.pl";
> }
> </script>
> ...
> <a href="noJS.htm" onClick="NewPage(); return false;">
> ...
...

<a href="noJS.htm" onclick="NewPage();return false;">

is the way to do it. I suggest you try it with:

<script type="text/javascript">
function NewPage() {
alert('here');
window.location.href = 'http://www.yahoo.com';
}
</script>
...
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 23 '05 #5
Mike wrote:
Perhaps I should first clarify what I mean by "clicking the browser
option to open in a new window." By this I mean, with IE or NS, I
right click on the <a> object, and select "Open Link in New Window"
from pulldown.
And what about pressing the Shift or Ctrl key while clicking the link
or pressing the Return/Enter key? Have you ever heard of Tabbed Browsing?
Have you ever heard of Opera aso.? Have you ever heard of text browsers
like `lynx' or `links'? Have you ever heard of tools for disabled people
like screen readers and Braille lines?
Do I need to detect this differently than via onClick. Do I need
to detect the right mouse, and then figure out which of the pulldown
elements they asked for?


Much you have to learn, young apprentice. Do you really think that
crippling the browsing tool of your visitors via a technology that can be
restricted, disabled or not even present is a Good Thing? Do you really
think that every system has a pointing device? Do you really think that
every visitor makes use of such a device? Do you really think that every
visitor uses the same software (OS, UA, UA version)? Do I need to continue?

If you code for the Web, first you need to learn how the Web works.
PointedEars
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Radermacher | last post by:
Hi, the following code has the mentioned behaviour: <html> <head> <script type="text/javascript"> function newA() { for (i=0;i<3;i++) { var a = window.frames.document.createElement("a");
11
by: Dana Smith | last post by:
Can anybody explain why when I have a hyperlink on a page with the code similar to below, after the user clicks on the link, the desired window opens but the page with the hyperlink changes to a...
14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
9
by: Kreso | last post by:
i'm passing the asp parameters using the url current page is files.asp and I'm using window.location.href=files.asp?action=deletefile to pass the action to the serverside My code never got...
2
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display...
53
by: usenet | last post by:
See <ul> <li><a name="link1" onClick="alert(this.name);return false;" href="#">Link1</a></li> <li><a name="link2" href="javascript:alert(this);">Link2</a></li> <li>Item 3</li> </ul> ...
1
by: nsvmani | last post by:
Hi, i am trying to get the FileOpen dialogue window as soon as clicked href link I am using IE6 with ActiveX enabled. Just need to get the File Open dialogue window when i click on the HREF links.It...
2
by: Wolfman | last post by:
Hi guys I recently added a popup HREF script to a page, but I'd also like to make the page itself change when the HREF command is clicked. When the user clicks, not only would the popup window...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.