SmartestVEGA
SmartestVEGA

Reputation: 8899

How to replace url in script source

There is a url

/Kentico9/CMSPages/GetResource.ashx

in the following script,

  <script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fRequireJS%2frequire.js" type="text/javascript"></script>
<script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fRequireJS%2fconfig.js&amp;resolvemacros=1" type="text/javascript"></script>
<script src="/Kentico9/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fcms.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
if ((window.originalPostback == null) && (window.__doPostBack != null)) { window.originalPostback = __doPostBack; __doPostBack = __doPostBackWithCheck; }

//]]>
</script>
<script src="/Kentico9/ScriptResource.axd?d=_9yHV47QJb18THQ6kRwtMTYWP8AyLTDDz_ezsjVynWQhicLV_U3iBRnjAic5MX-xDgyPX48_xtLVYXhKOv2UCJKAoTTMC4wGhtJzijblJUqnor1iJ4U59KPu7436hU-u0&amp;t=7c776dc1" type="text/javascript"></script>
<script src="/Kentico9/ScriptResource.axd?d=zf3zdXaB_cJmg3ZI845HWFeB9wwz6hDKzOk9u8r8LRzjBXOxGqGc8ov1CG1yunKlRYOyRHSZ9KBtNMB3nu1nMQXXiYklnIFMhWV0Xj3pkcNu0JnN6rQtu7_ee21y6R8Tp2tmpXsVH8ZTIabIz8lDAA2&amp;t=7c776dc1" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var CMS = CMS || {};
CMS.Application = {
"isRTL": "false",
"isDebuggingEnabled": true,
"applicationUrl": "/Kentico9/",
"imagesUrl": "/Kentico9/CMSPages/GetResource.ashx?image=%5bImages.zip%5d%2f",
"isDialog": false
};

I need to change this url

/Kentico9/CMSPages/GetResource.ashx 

to

http://localhost/Kentico9/CMSPages/GetResource.ashx

I tried the following script to replace ,which is not working.

var res = "entire html source shown above";
res.replace('/Kentico9/', 'http://localhost/Kentico9/');

How i make this working?

Upvotes: 0

Views: 1503

Answers (3)

elreeda
elreeda

Reputation: 4597

Use this

var remove = "/Kentico9/CMSPages/GetResource.ashx";
var newLink = "http://localhost/Kentico9/CMSPages/GetResource.ashx";
$('script').each(function(){
  var link = $(this).attr('src');
  $(this).attr('src', link.replace(remove, newLink));
})

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075269

Two things:

  1. replace returns the new string.

  2. When you pass a string as the first argument, it only replaces the first instance, not all of them. to replace all, you need a regular expression with the g flag.

So:

res = res.replace(/\/Kentico9\/CMSPages\/GetResource\.ashx/g, 'http://localhost/Kentico9/CMSPages/GetResource.ashx');
//  ^-- assign    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
//                regex                                    global

Note that the / and the . in the regular expression are escaped, as otherwise they have special meaning in the regex.

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68433

try it specifically for imageURL

res.replace('"imagesUrl": "/Kentico9/', '"imagesUrl": "http://localhost/Kentico9/');

Upvotes: 1

Related Questions