Videogamer555 Posted January 30, 2013 Share Posted January 30, 2013 All 4 of the below pieces of code have the same linking affect, with no differences that I can see (I'm using the latest version of Firefox as my browser), but one of them must be the "correct" technique. <img src="mypic.jpg" onclick="document.location='http://www.google.com'" /> <img src="mypic.jpg" onclick="window.location='http://www.google.com'" /> <img src="mypic.jpg" onclick="self.location='http://www.google.com'" /> <img src="mypic.jpg" onclick="location='http://www.google.com'" /> Which one is the official correct techniuqe for using JS for turning a part of a web page (such as an image) into a link? Link to comment Share on other sites More sharing options...
Ingolme Posted January 30, 2013 Share Posted January 30, 2013 The best idea would be to wrap the <img> tag in an anchor: <a href="http://www.google.com/"><img></a> In Javascript, document.location doesn't exist, window.location and location are the same thing, self.location has to do with frames which you shouldn't need to worry about. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2013 Share Posted January 30, 2013 Even though it works to set the entire location object to a string, you should actually set window.location.href. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now