javascipt target blank

gaptrast

Member
Messages
123
Reaction score
0
Points
16
hi,

I am using the onclick attribute instead of <a>.

in <a> I can just set the target to _blank


but how can i do that with <img>? this does not work: <img target="_blank" onclick"..." />

HOW?
 

wplounge61

New Member
Messages
1
Reaction score
0
Points
0
Here's how:
<img src="image.jpg" onclick="window.open('http://x10hosting.com/')" />
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Using an onclick doesn't open a URL, so the concept of "target" doesn't really apply. What are you doing? Please post a minimal test case.
 

gaptrast

Member
Messages
123
Reaction score
0
Points
16
hi, sorry misson...

I used onclick="window.location('...')"

Thanks wplounge61 your answer solved my question.:)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Why are you using JS with a non-anchor rather than the semantically correct anchor (<a>) elements? HTML should define the document structure. This is very important because computers, having no intelligence, need cues in order to deal with data. Elements are cues as to the meaning of the data contained within. If data is a link, it should be marked as such. You can still incorporate JS if necessary, but it shouldn't be relied on, and it fulfills a different purpose.

More concretely: if you use JS rather than links, it will affect programs such as screen readers, browsers that don't support JS (such as on some cell phones) and search spiders, making it harder to index your site or reducing its page rank. There are also plenty of people who have JS disabled who won't be able to use your site.

Basically, using a JS link and a non-anchor element is the Wrong Thing to do. Read "Accessible Pop-up Links" on A List Apart and "Links & JavaScript Living Together in Harmony" on Evolt for better ways (note that these are only a few articles of many; search the web for others).

There is a suggestion for HTML5 to allow any element to function as a link, but it hasn't yet been submitted to the W3C.
 
Last edited:
Top