简体   繁体   中英

I want to assign one variable twice but only want to use first one in javascript

There is a file name vicidial.php in vicidia file a value is assigned

var DiaLControl_auto_HTML = 'something' 

I dont want to edit in this file but i want to edit this variable from other file so i created file_js.js but when i assigned var DiaLControl_auto_HTML = 'my_value' its only use first one eg. something i want to override my variable. how can i do this?

  <script src="file_js.js"></script>
  in vicidial.php
  var DiaLControl_auto_HTML = 'default value';


  in file_js.js
  var DiaLControl_auto_HTML = 'my_value';

In vicidial have:

<script>
var DiaLControl_auto_HTML = 'default value';
</script>
 <script src="file_js.js"></script>

in file_js.js

window.addEventListener("load",function() {
  window.DiaLControl_auto_HTML = 'my_value';
})

this is a workable code, try it. I hope it can help you.

main html

<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Test Var</title>
  <script src="file_js.js"></script>
</head>
<body>

    <br>
    Value before : <span id="valueBefore"></span><br>
    <br>
    Value after : <span id="valueAfter"></span><br>
    <br>

   <script>

    var DiaLControl_auto_HTML="default value";


    // the function() wil be executed when your page is loaded
    // better use readystate==complete, but it is a another problem...
    window.addEventListener("load",function() {


     // set var  
     DiaLControl_auto_HTML = 'my_value';
     // show it in browser
     document.getElementById('valueBefore').innerHTML=DiaLControl_auto_HTML;

     // call method on file_js 
     myOwnFunction();
     // show it in browser
     document.getElementById('valueAfter').innerHTML=DiaLControl_auto_HTML;

    })
   </script>
</body>
</html>

file_js.js

 // file_js.js FILE
 function myOwnFunction () {
     DiaLControl_auto_HTML="Value changed";
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM