473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple question by beginer

I am new to Javascript and have a fairly straightforward question. I
am trying to use an image as a link to open a new page with the
onmouseclick event. In general this seems to work fine with the open
statement.

I wish to use the same script at various places in my web and hence
wish to pass to the javascript function the URL location and the
width and height of the new page. I am having no luck trying to get
the open statement to work with passed arguements.

After doing a little research it seems I should be using the eval
statement, but this does not work. What am I doing wrong, and should
I be passing arguements to the open statement a different way?

The web page is at www.snowfisher.com/test.htm

Sep 19 '07 #1
7 1660
On Sep 19, 12:34 pm, Helpful person <rrl...@yahoo.c omwrote:
I am new to Javascript and have a fairly straightforward question. I
am trying to use an image as a link to open a new page with the
onmouseclick event. In general this seems to work fine with the open
statement.
No JavaScript required:
<a href="img-src.jpg" target="_blank" ><img src="img-src.jpg"
alt="something" ></a>

The "target" attribute isn't valid XHTML, but you're not going for
that anyway.
After doing a little research it seems I should be using the eval
statement, but this does not work. What am I doing wrong, and should
I be passing arguements to the open statement a different way?
You should actually AVOID using the "eval" statement, until you have a
better understanding of its power and pitfalls. For most uses, there
are better workarounds.

In this case, your problem is the fact that you have a URL outside of
a string. What you mean is not:

function openwindow2(ima ge_location,awi dth,aheight) {
x = "window.ope n(" + image_location + ",'_blank', " +
"'width=200,hei ght=100')"
alert(x)
eval( x)
}

but:

function openwindow2(ima ge_location, awidth, aheight) {
window.open(ima ge_location, '_blank', 'width=200,heig ht=200');
}

Using "eval" here is quite overkill, where a simple function call will
suffice.

-David

Sep 19 '07 #2
On Sep 19, 3:49 pm, David Golightly <davig...@gmail .comwrote:
On Sep 19, 12:34 pm, Helpful person <rrl...@yahoo.c omwrote:
I am new to Javascript and have a fairly straightforward question. I
am trying to use an image as a link to open a new page with the
onmouseclick event. In general this seems to work fine with the open
statement.

No JavaScript required:
<a href="img-src.jpg" target="_blank" ><img src="img-src.jpg"
alt="something" ></a>

The "target" attribute isn't valid XHTML, but you're not going for
that anyway.
After doing a little research it seems I should be using the eval
statement, but this does not work. What am I doing wrong, and should
I be passing arguements to the open statement a different way?

You should actually AVOID using the "eval" statement, until you have a
better understanding of its power and pitfalls. For most uses, there
are better workarounds.

In this case, your problem is the fact that you have a URL outside of
a string. What you mean is not:

function openwindow2(ima ge_location,awi dth,aheight) {
x = "window.ope n(" + image_location + ",'_blank', " +
"'width=200,hei ght=100')"
alert(x)
eval( x)

}

but:

function openwindow2(ima ge_location, awidth, aheight) {
window.open(ima ge_location, '_blank', 'width=200,heig ht=200');

}

Using "eval" here is quite overkill, where a simple function call will
suffice.

-David
The target attribute is not valid html strict which is one of the
reasons I need to use javascript.

I am not surprised that eval is not the correct solution. However,
how do I pass the width and height parameters to the open statement?
Sep 19 '07 #3
On Sep 19, 12:55 pm, Helpful person <rrl...@yahoo.c omwrote:
The target attribute is not valid html strict which is one of the
reasons I need to use javascript.
In that case, don't use target, then. Open the link in the current
window, and attach a JavaScript click event handler to the anchor tag
to prevent this from happening, should JS be enabled.
I am not surprised that eval is not the correct solution. However,
how do I pass the width and height parameters to the open statement?
With my above considerations, here's how this would look:

function openNewWindow(i mage_location, awidth, aheight) {
window.open(ima ge_location, '_blank', 'width='+awidth
+',height='+ahe ight);
return false; // prevents default behavior
}

and your HTML tag:

<a href="img-src.jpg" onclick="openNe wWindow(this.hr ef, 300,
200);"><img src="img-src.jpg" alt="my picture"></a>

-David

Sep 19 '07 #4
On Sep 19, 4:11 pm, David Golightly <davig...@gmail .comwrote:
On Sep 19, 12:55 pm, Helpful person <rrl...@yahoo.c omwrote:
The target attribute is not valid html strict which is one of the
reasons I need to use javascript.

In that case, don't use target, then. Open the link in the current
window, and attach a JavaScript click event handler to the anchor tag
to prevent this from happening, should JS be enabled.
I am not surprised that eval is not the correct solution. However,
how do I pass the width and height parameters to the open statement?

With my above considerations, here's how this would look:

function openNewWindow(i mage_location, awidth, aheight) {
window.open(ima ge_location, '_blank', 'width='+awidth
+',height='+ahe ight);
return false; // prevents default behavior

}

and your HTML tag:

<a href="img-src.jpg" onclick="openNe wWindow(this.hr ef, 300,
200);"><img src="img-src.jpg" alt="my picture"></a>

-David
Thank you very much. I'll try this when I have time.

Sep 19 '07 #5
On Sep 20, 6:11 am, David Golightly <davig...@gmail .comwrote:
[...]
With my above considerations, here's how this would look:

function openNewWindow(i mage_location, awidth, aheight) {
window.open(ima ge_location, '_blank', 'width='+awidth
+',height='+ahe ight);
return false; // prevents default behavior
Only if that value is returned by the onclick handler itself. I guess
stricktly that should be:

if (window && typeof window.open == 'function'){
window.open(... );
return false;
}

Though it is likely not necessary.

}

and your HTML tag:

<a href="img-src.jpg" onclick="openNe wWindow(this.hr ef, 300,
To cancel navigation, pass the returned value to the onclick handler:

<a ... onclick="return openNewWindow(. ..);">
Otherwise navigation isn't cancelled.
--
Rob

Sep 20 '07 #6
RobG wrote:
if (window && typeof window.open == 'function'){
Will not work in MSHTML where `typeof' for DOM methods yields
"object". Hence my writing isMethod() and isMethodType() in
http://PointedEars.de/scripts/types.js
Regards,

PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Sep 20 '07 #7
On Sep 19, 6:18 pm, RobG <rg...@iinet.ne t.auwrote:
To cancel navigation, pass the returned value to the onclick handler:

<a ... onclick="return openNewWindow(. ..);">

Otherwise navigation isn't cancelled.
Right, good catch.

-David

Sep 20 '07 #8

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

Similar topics

2
6081
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at the bottom of every PHP page. The board should have just the basic features, like: - posting capability
8
6507
by: Dan | last post by:
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward?
7
2289
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request that page like http://machinename/dir1/hellp.aspx instead of running that page it starts downloding ...whats missing here ....why the aspx engine not running the page....
0
1242
by: mionix | last post by:
Hi I'm beginer with php. I need a favour. Could somebody show me or send to my email source code with simple home page, something like this: table with three rows: first row (headline - logo) second row (table with three columns - left, middle, right)
1
1557
by: itsjyotika | last post by:
Hello Everyone, I need to read data from a CVS file(i created it from micosoft excel) and then need to match it with the one of the date from the command line.If the date is there then it should say yes or else it should say no. I am not getting how to do this as i am just a beginer in perl. Can anybody help me in writting the code. the files r toooo.. big to be attached , so i have just shown a tiny portion of it. thanks in advance, rimjim...
5
3232
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" ) cout << "First character is s" << endl; OR: string str("string"); string::iterator it = str.begin();
14
2989
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
1
1246
by: hamed steph | last post by:
i'm a beginer in programing and i need very much your help .i need explanation about while statement (loop initialization and structure of while ) also qualifier (long,short,unsigned ,signed,) type of variable (character and double). think you.......
10
2139
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.