简体   繁体   中英

My javascript is not being executed in my safari browser

I am using Safari and the Atom text editor. I have a basic html file with javascript code between script tags. However when I click on the folder that the code is saved in none of my javascript code is working in the browser, nor my Chrome browser. I double checked that javascript was enabled and that there were no plug-ins causing it to not run. I am trying to make an alert if the user's value is 0 and substitute the for the value entered into the text box once the "Click Me" button is clicked.

When I use an "onload...alert" attribute in the body element , the javascript worked perfectly.

Is it the text-editor? My code? Any ideas?

My code is below:

 function substitute() { var myValue = document.getElementById('myTextBox').value; if (myValue.length == 0) { alert ('Enter a Real Value'); return; } var myTitle = document.getElementById('title'); myTitle.innerHTML=myValue; } 
 <!doctype html> <html> <head> <title>Javascript Example</title> </head> <body> <h1 id="title">Javascript Example</h1> <form> <input type="text" id="myTextBox"/> <input type="submit" value="Click Me" onclick="substitute"/> </form> </body> </html> 

For starters, are you linking the JS file anywhere in your html? At the end of your body tag, you should add something like this:

<script src="script.js"></script>

Also, 'substitute' is a function so it should be 'substitute()'

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