henryaaron
henryaaron

Reputation: 6202

Strip URL of JavaScript

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!

Upvotes: 0

Views: 178

Answers (2)

Ry-
Ry-

Reputation: 225074

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.

Upvotes: 2

user1000131
user1000131

Reputation:

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];

Upvotes: 2

Related Questions