How to change the value of a variable that is within a function through another function?

Asked

Viewed 38 times

-2

I have seen similar questions here in the OS, but I still can not solve the following situation, where I have the function below that is inside an external script:

function updateScript(marcador, a, b) {
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.id = widget_config.script_id;
        script.src = widget_config.home_page + '/feeds/posts/default/-/' + marcador + '?alt=json-in-script&start-index=' + a + '&max-results=' + b + '&callback=grabList';

    if (document.getElementById(widget_config.script_id)) {
        var oldScript = document.getElementById(widget_config.script_id);
        oldScript.parentNode.removeChild(oldScript);
    }
    head.appendChild(script);
}

My purpose is, within my HTML file, to add a different value to the variable marcador depending on where I call the possible function that passes this string to the marcador. Ex.: Somewhere, I want to define the text CSS as the marker; in another, the text HTML, and so on.

In case, my HTML is like this:

<script src="endereço-do-script-acima.js"></script>
<script>updateScript("nome-do-marcador")</script>

It is possible?

  • You don’t do that, it doesn’t make sense, you probably want to do something else. If you do, I’ll come back later.

  • All right, @Maniero If you can help me, man, I’m grateful!

  • 1

    Setting as function parameter will not suit you?

  • @Geeksilva I tried as parameter but it didn’t work. I updated the question to make it clearer how my code is so far.

  • Has any Exception been launched? I still don’t quite understand the goal.

  • My intention, in fact, is to avoid repeating a large number of code, @Geeksilva . I have 8 Divs to show in each of the publications of a certain marker. I have a script that does this "categorization", because I just put the name of the marker inside the variable script.src that works. Only I’m trying a way to isolate this variable with the name of the marker and make it dynamic, because otherwise I’ll need to use all the code 8 times, and if I change only this line, it solves my problem.

  • Now I understand. When using the parameter, what didn’t work?

  • @Geeksilva The console says the parameter was not found.

  • You’d have to put that code on JS Fiddle?

  • I can send, yes, @Geeksilva It seems I’ve discovered a possible reason for the error. It seems to me that the code only accepts an ordered list and a div with specific ID. I am doing the tests here and any news, warning. Thank you!

  • Beauty. Because it is very strange that he does not recognize the parameter.

  • @Geeksilva Really, man, the problem I’m having is that the code only works in one instance. I was able to isolate the variable marker, declaring it externally (which was my goal with the question), but I won’t be able to close the 8 Divs because the code merges these markers into a single DIV, but that’s another story. For all intents and purposes, the question itself already has an answer. Thank you very much for your helpfulness, boy!

  • Imagine. How much we need

Show 8 more comments

1 answer

1

Some errors were introduced to me for not having its full HTML and not being able to catch some elements by the ID, but I believe that this code will work.

function updateScript(marcador, a, b) {
    var head = document.getElementsByTagName('head')[0],
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.id = widget_config.script_id;
        script.src = widget_config.home_page + '/feeds/posts/default/-/' + marcador + '?alt=json-in-script&start-index=' + a + '&max-results=' + b + '&callback=grabList';

    if (document.getElementById(widget_config.script_id)) {
        var oldScript = document.getElementById(widget_config.script_id);
        oldScript.parentNode.removeChild(oldScript);
    }
    head.appendChild(script);
}

function opcaoSelecionada(val){
  
  if(val){
    updateScript(val, 0, 10)
  }
}
<head>
  <select onchange="opcaoSelecionada(this.value)">
    <option value="">Selecione uma opção</option>
    <option value="HTML">HTML</option>
    <option value="CSS">CSS</option>
  </select>
</head>

  • Hi, @Kodark Thanks for the answer! I was able to solve the problem by declaring the variable 'marker' inside the HTML, outside the script. Thank you for your help, my dear!

Browser other questions tagged

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