I was wondering if there’s a better way to execute a link’s href attribute other than evaling it?
For example:
eval($("a[href^=javascript:]").attr("href").replace(/^javascript:/,"")); // better (more eficient) way to do this?
Thanks!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re talking about arbitrary JS code embedded in a
href="javascript:window.open('blah')"style link, then not really.You should clarify the question though –
hrefson anchor tags are really meant to be links to other documents, and you don’t run them, you navigate to them. All you’re really asking in your question is how to execute arbitrary javascript code supplied as a string, which is both more accurate and makes the answer clearer. (If you try toevala standard URL href attribute, nothing interesting will happen.)If you did mean “how do I get the same behaviour that would happen if I clicked on this link”, then that’s pretty complex. There’s no specific way to do it, so you have to try to emulate the effects by reading the attributes of the link and synthesizing an equivalent action in Javascript. If you forget to inspect the
targetattribute, for example, this just won’t work the same, and I’m sure there are lots of other pitfalls too.