How to replace <script> tag data with jQuery?

Asked

Viewed 480 times

3

I’m trying to replace a certain value within a HTML parseado, using the following function I can override values that are within inputs tags, css and etc:

$('input[value="/services/CouponTemplate.mvc/GetCoupon"]').attr('value', function(_, href){
    return "https://meusite.com?url=https://siteremoto.com" + href;
});

Only now I need to replace an address inside the tag <script type="text/javascript">. It’s like this:

<script type="text/javascript">var inPlayPreferences={"MarketOrder":,"InPlayAppServiceName":"/services/InPlayApp.mvc/"};</script>

I need to replace the /services/InPlayApp.mvc/, i can put PHP with str_replace, but I wonder if you can do the same using jQuery. I tried to repeat the procedure by changing the input for script but it didn’t work, I’m a beginner in the Javascript world. There is something similar to str_replace PHP in jQuery?

  • 1

    I think that should really be done on the server side. If you have to change on the client side it is option to simply give a new value to the variable?: inPlayPreferences.InPlayAppServiceName = "/services/novoNome.mvc/";

  • I didn’t understand why. Couldn’t you just assign a new value to the array? inPlayPreferences["Inplayappservicename"] = "new value here..";

  • The data of InPlayAppServiceName are automatically generated by the remote site, if I assign a new value to it, simply the script would stop working, because the code is large and contains instructions in base_64 that is generated on each page differently, just summarized the code to facilitate the explanation. Already the question of Sergio, I’m doing it right on the server side, but I was wondering if it could be done on the client side, just out of curiosity even.

1 answer

2


You can assign an identifier to this script tag, so it’s easier to find it, e.g.:

<script id='scriptQueSeraAlterado' type='text/javascript'></script>

With jQuery:

$(function(){
  
  var script = $('#theScript'); // alvo
  var content = 'var bar = 10;'; // conteúdo que será inserido dentro da tag
  
  $('#changeScriptContent').on('click', function(){
    alert('Antes de alterar:\n' + script.get(0).outerHTML);
    script.html(content); // :)
    alert('Depois de alterar:\n' + script.get(0).outerHTML);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script id='theScript'>
    var foo = 2;
</script>

<button id='changeScriptContent'>Alterar</button>

Javascript-only:

var button = document.getElementById('changeScriptContent'), // botão que altera o conteúdo
    script = document.getElementById('theScript'), // alvo
    content = 'var bar = 10;'; // conteúdo que será inserido

button.addEventListener('click', function(){
    alert("Antes de alterar:\n" + script.outerHTML);
    script.innerHTML = content; // :)
    alert("Depois de alterar:\n" + script.outerHTML);
});
<script id='theScript'>
    var foo = 2;
</script>

<button id='changeScriptContent'>Alterar</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.