简体   繁体   English

我想分配一个变量两次,但只想在 javascript 中使用第一个变量

[英]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 vicidia文件中有一个文件名vicidial.php 赋值

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.我不想在这个文件中编辑,但我想从其他文件编辑这个变量,所以我创建了 file_js.js 但是当我分配 var DiaLControl_auto_HTML = 'my_value' 它只使用第一个,例如。 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:在vicidial有:

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

in file_js.js在 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主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文件_js.js

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想用一个按钮打开/关闭菜单,我只想使用 Javascript - I would like to open/close a menu with one button, I want to use only Javascript 当我想要两个按钮时,我的 javascript 函数中只有一个按钮 - Only one button in my javascript function when I want two 我想使用一个相同的按钮来播放和暂停。第一次单击我要播放它,第二次单击我要暂停它 - i want to use one same button to play and pause.First click I want to play it & second click I want to pause it 我想将javascript变量分配给javascript中的html元素 - I want to assign javascript variable to html element inside javascript getElementsByClassName 但只操作我想要的那个 - getElementsByClassName but manipulate only the one that i want 我想在动态创建文本框时将一个文本框值分配到 javascript 中的另一个文本框 - I want to assign the one textbox value into another textbox in javascript in dynamic creation of textbox 我想将第一个函数组合到第二个函数,所以我只能调用它一次 - I want to combine first function to the second one, so I can call it only once 我希望我的 javascript 在第一个 if 语句评估为 TRUE 时移动到下一个 if 语句 - I want my javascript to move to the next if statement if the first one evaluates as TRUE 如何撤消 JavaScript function? 第一个 function 有效,但我想用第二个 function 撤消它,但那个不起作用 - How to undo a JavaScript function? The first function works, but I want to undo it with the second function, but that one does not work 我在 React 中创建了折叠,我为每次点击使用了不同的 useState,但我只想使用一个 useState - I created collapse in React, I used different useState for each click, but I want to use only one useState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM