admin管理员组文章数量:1422062
I have one test.html
file with two <script>
tags. I need to share a variable from one to another..
Sample code:
<script type="text/javascript">
var test = false;
function testing() {
test = true;
alert('I am inside..');
}
testing();
</script>
...
<script type="text/javascript">
if (test == true) {
alert('working');
} else {
alert('failed');
}
</script>
I have one test.html
file with two <script>
tags. I need to share a variable from one to another..
Sample code:
<script type="text/javascript">
var test = false;
function testing() {
test = true;
alert('I am inside..');
}
testing();
</script>
...
<script type="text/javascript">
if (test == true) {
alert('working');
} else {
alert('failed');
}
</script>
The output is always:
I am inside..
failed
I also tried to use the window
class but it doesn't matter.. (window.test
)
What I have to do to get the 'working' alert?
Thanks if anyone can help me. I saw some similar questions, but the answers wasn't a solution for me.
EDIT:
The original code (simplified):
<head>
...
<script type="text/javascript" src="detectblocker.js"></script>
<!-- GitHub: https://github./sitexw/BlockAdBlock/ -->
...
</head>
<body>
<script type="text/javascript">
var blocker = false;
function adBlockDetected() {
blocker = true;
alert('inside');
}
if(typeof blockAdBlock === 'undefined') {
adBlockDetected();
} else {
blockAdBlock.onDetected(adBlockDetected);
}
blockAdBlock.setOption({
checkOnLoad: true,
resetOnEnd: true
});
</script>
<div class="header">
...
</div>
<div class="content_body">
<div class="requirs">
<ul>
...
<script type="text/javascript">
if (blocker == true) {
document.write("<li>enabled!</li>")
} else {
document.write("<li>disabled!</li>")
}
</script>
...
</ul>
</div>
</div>
...
</body>
The output is an alert()
"inside" and the <li>
"disabled".. (Blocker is enabled..).
The only difference I can see is on the end of the first <script>
tag:
blockAdBlock.setOption({
checkOnLoad: true,
resetOnEnd: true
});
So why the snippet is working and my code not? Confusing...
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 2, 2016 at 13:32 vP3nguinvP3nguin 3306 silver badges18 bronze badges 20- 1 I converted your snippet to a live demo and … it works. Whatever the problem is, you haven't exposed it in your question. – Quentin Commented Dec 2, 2016 at 13:36
- 1 @xShirase — The problem never appeared in the first place. The edit just highlights that it never appeared. – Quentin Commented Dec 2, 2016 at 13:39
- 1 i copied that code and tried .That works perfect .where is the issue? – geekbro Commented Dec 2, 2016 at 13:40
- 2 @13loodH4t — After your edit … it still outputs "enabled" so you still don't seem to have provided a minimal reproducible example – Quentin Commented Dec 2, 2016 at 14:24
-
1
If you hit the
else
branch ofif(typeof blockAdBlock === 'undefined')
andblockAdBlock.onDetected
is asyncronous (which it sounds like it is) then this is a duplicate of this question. Since you haven't provided a minimal reproducible example it isn't possible to tell for sure though. – Quentin Commented Dec 2, 2016 at 14:27
3 Answers
Reset to default 2If you do not use var
before a variable it bees a global variable like
test = true;
The variable test
will be true during the page and also in your next scripts and functions.
Try this:
<script type="text/javascript">
var test = false;
function testing() {
var test = true;
alert('I am inside..');
}
testing();
</script>
...
<script type="text/javascript">
if (test == true) {
alert('working');
} else {
alert('failed');
}
</script>
There are two ways of doing it.
1) create a hidden element and set your variable from your first script to attribute of that element.
This is your hidden element
<input type="hidden" id="hiddenVar"/>
and can set it in javascript as
document.getElementById("hiddenVar").setAttribute("myAttr",test)
Now you can get it in next script as
document.getElementById("hiddenVar").getAttribute("myAttr")
2) By .data() you can read about it here
Using window
's global variables may be what some are looking to do. You can read more in this answer, or the docs.
<script type="text/javascript">
window.test = 'some text';
</script>
...
<script type="text/javascript">
alert(window.test);
</script>
...
<script type="text/babel">
alert(window.test);
</script>
本文标签: scopeJavascriptShare Variables over different ltscriptgt tagsStack Overflow
版权声明:本文标题:scope - Javascript - Share Variables over different <script> tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745330199a2653776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论