Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

How can I define variables in a JavaScript file within an html file in which the JavaScript is used?

I have searched for an answer, but I haven't found a answer that address my issue, as I'm not how to word it as a search. My problem is that I want to use the same JS code for multiple HTML files, but only a few values need to be changed in the JS depending on the HTML file.

HTML

<!DOCTYPE html>
<!--Replace:: Line 14, Line 82: 'PROJECT' with name of the project; Line 15: '0' with the last counted image; line 94: 'PROJECT' with base name of picture files-->
<!--fill in any special instructions at each step in the text array.-->
<html>
<head>

<link type="text/css" rel="stylesheet" href="../defaultStyle.css"/>

<script type="text/javascript" src = "../buttons.js">

var num = 0
var NAME = PROJECT
var LAST = 100
var LAST1 = LAST-1

text[0] = "a"
text[1] = "b"
<!--...etc-->

</script>

    <link type="text/css" rel="stylesheet" href="../projectStyle.css"/>

</head>

<body>

    <div>
        <p href="JavaScript:StepDisplay()" id="Step">PROJECT</p>
    </div>

    <div id="Text">
    </div>

    <div>
        <button type="button" onClick="JavaScript:Back()"> Back</button>
        <button type="button" onClick="JavaScript:Next()"> Next</button>
    </div>

    <div>
        <img src="image/PROJECT (0).jpg" id="Pic">
    </div>

</body>
</html>


***JavaScript***

var text = new Array[100]
var num = 0
var NAME = PROJECT
var LAST = 100
var LAST1 = 99

function Next(){
    num = num + 1
    if (num == LAST)
    {num = 0}
    stepnum = num.toString();
    document.getElementById("Pic").src = "image/"+ NAME + "(" + stepnum + ").jpg"
    document.getElementById("Text").innerHTML = text[num]
    if (num == 0)
    document.getElementById("Step").innerHTML = NAME
    if (num == 1)
    document.getElementById("Step").innerHTML = "Pieces"
    if (num > 1)
    document.getElementById("Step").innerHTML = "Step " + (num-1).toString();
}

function Back(){
    num = num - 1
    if (num < 0)
    {num = LAST1}
    stepnum = num.toString();
    document.getElementById("Pic").src = "image/" + NAME +  "(" + stepnum + ").jpg"
    document.getElementById("Text").innerHTML = text[num]
    if (num == 0)
    document.getElementById("Step").innerHTML = NAME
    if (num == 1)
    document.getElementById("Step").innerHTML = "Pieces"
    if (num > 1)
    document.getElementById("Step").innerHTML = "Step " + (num-1).toString();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should define your local variables before including the script.

<script>
    // local variables
    var firstname = "John", lastname = "Doe";
</script>
<script src="hellohello.js"></script>

In the js file, we need to make sure our local variables exists, if it doesn't we can define default variables or stop the script.

// Check if the variable already exists

// If it doesn't we'll give it a default variable
if(typeof(firstname)==='undefined') firstname = "Guest";
if(typeof(lastname)==='undefined') lastname = "";
alert("Hello "+ firstname +" "+ lastname);

// Or just do something else
if(typeof(firstname)!=='undefined' && typeof(lastname)!=='undefined') {
   // Do something
} else {
   // Do nothing
}

Demo 1: http://bycreativeminds.com/playground/vanilla/js-demo-110.html
Demo 2: http://bycreativeminds.com/playground/vanilla/js-demo-111.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...