简体   繁体   中英

Working with external JavaScript functions and HTML

Thank you in advance for helping me with my issue. I was wondering if someone could explain this for me as i am self teaching about JavaScript JQuery. What i am trying to do is push a message into a variable that is in the parameters of a function. Then i want to display the message using the function by calling it back to the html file that i originally had. So i want write a message in my function and print to my html page. I think i maybe in the right direction here is my source code. I am trying to get use to write with external javascript files as i think it is much cleaner way of working with HTML, CSS3, and JavaScript.

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8">
 <title></title>
 <script type="text/javascript" src=".js"></script>
 <link rel="stylesheet" type="text/css" href="mystyle.css">
 </head>
 <body>

<form>


    <input type="button" value="Click me" id="message "onclick="test();" />

</form>

</body>
</html>

External JavaScript file.

 function test(message){


      alert("Hello");


 }

Use this.value to get the value of the input

<input type="button" value="Click me" id="message" onclick="test(this.value);" />

And then use the message you get

function test(message){

    alert(message);

}

To show this message on your page, create an element on the page

<div id="message"></div>

Then, in your test function, instead of alert , set the innerText or innerHTML to the message

document.getElementById('message').innerText = message;

To show message in popup do it this way

Html:

<input type="button" value="Show message in popup" id="message "onclick="test2('hii this is my message');" />

JS:

function test2(message)
{
 alert(message);   
}

To show message in div

Html:

 <input type="button" value="Show message in div" id="message "onclick="test('hii this is my message');" />
 <div id="message"> </div>

JS

function test(message)
{
    document.getElementById("message").innerHTML = message;
}

Demo : Fiddle

Adding the message: The easy way to do this would be by using jQuery or a javascript library of your choice. Otherwise, you can simply do this:

Create an empty div to display the msg with an id="msg"

and in your javascript function:

document.getElementById("msg").innerHTML = "Your message";

jQuery is much more fun: you can use readily available functions like .html() and .append()

You definitely want to do this, as it is much MUCH cleaner to do so. It also lets you use the same code in multiple places.

Look at this: http://www.w3schools.com/js/js_whereto.asp

(down by 'external JavaScript')

Essentially, you need to save the .js file as an actual file, like filename.js :

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

where filewithcode.js is the file path to the JavaScript file you have created.

Hope this helps

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