0

I have a URL that contains a lot of JavaScript.

I want to strip the URL of all JavaScript using of course JavaScript!

Here's my HTML

<a id="product_photo_zoom_url" href="javascript:OpenNewWindow('/PhotoDetails.asp?ShowDesc=N&PhotoNumber=3&ProductCode=TCP65GT30', 640, 480); void(0);" title="" rel="lightbox">

Basically I'd like the URL to just look like this:

 <a id="product_photo_zoom_url" href="/PhotoDetails.asp?ShowDesc=N&PhotoNumber=3&ProductCode=TCP65GT30" title="" rel="lightbox">

There are many instances of this so trimming off the first and last ten characters would not be sufficient.

Get Element by ID product_photo_zoom_url would be fine.

Thank you for helping me!

1
  • Does the solution need to be in raw js? or are you using other js libraries such as jquery. Commented Jan 5, 2012 at 2:01

2 Answers 2

2

Just get the text between single quotes:

var href = "javascript:OpenNewWindow('/PhotoDetails.asp?ShowDesc=N&PhotoNumber=3&ProductCode=TCP65GT30', 640, 480); void(0);";
var url = /'(.+?)'/.exec(href)[1];

Demo

Also, you shouldn't use mutliple elements with the same id, it's invalid. ids are meant to be unique. You should be using classes instead. If you can, you should change them.

Sign up to request clarification or add additional context in comments.

9 Comments

How can I make this apply to anchors with the id product_photo_zoom_url?
Nice, very lean. You can modify that with JQuery to find all the anchor tags using something like $('a[href^="javascript"]').each(function(){})
@user1090389: Since you're using jQuery, try this: jsfiddle.net/minitech/QJQRa/1
Why would you use jQuery for anything but the line 4? You can delete the function, and replace that mess of $().prop...s with this.href=/'(.+?)'/.exec(this.href)[1];.
@skier88: Not enough jQuery :) I hate mixing the two anyway, doesn't feel right.
|
2

If you just want it to run on the one anchor, you can do this:

var el=document.getElementById('product_photo_zoom_url');
el.href=/'(.*?)'/.exec(el.href)[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.