I put this into Notepad and saved as an htm. However, when I open it in IE, it just says the text without the <HEAD> and </HEAD>. I have tried encoding it as Unicode, UTF-8, Unicode big Endian, and ANSI.:
<HEAD>
var ifbumper=0
if (ifbumper=0)
{
window.location='/bumper?url=whatever'
}
</HEAD>
You're writing JavaScript code, you need to put it inside of <script> tags. You also need to use == instead of = to compare values, and it's a good idea to put a ; following a line of code.
<HTML>
<HEAD>
<SCRIPT>
var ifbumper=0;
if (ifbumper==0)
{
window.location='/bumper?url=whatever';
}
</SCRIPT>
</HEAD>
You need to wrap JavaScript code inside <script> tags like this:
<head>
<script type="text/javascript">
var ifbumper = 0;
if (ifbumper == 0){
window.location='/bumper?url=whatever';
}
</script>
</head>
The equality operator is ==, not just =. Your if should be aif (ifbumper == 0)
Related
So basically I want to change image in every 5 second, so I wrote the following Javascript code and tag it to html. But the console keep saying that "No javaScript on this page" and the code does not apply
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript for Programmers</title>
</head>
<body>
<h2>Mood Change</h2>
<p>The mood of this web page changes every 5 seconds.</p>
<p><img id="mood" src="frown.gif" alt="mood"></p>
<script async src="../scripts/moody.js"></scripts>
</body>
</html>
var images=[]
images[0]="smile.gif";
images[1]="frown.gif";
var myMood= document.getElementById("mood");
function change(){
if(myMood==images[0]){
myMood.src=images[1];
}
else if(myMood.src==imgaes[1]){
myMood.src=images[0];
}
}
setInterval(change,5000);
</scripts> should be </script>
Also your JS could look like this: http://jsbin.com/nawono/2/edit
var myMood= document.getElementById("mood");
var images=[
"smile.gif",
"frown.gif"
];
function change(){
myMood.src= images.reverse()[0];
}
setInterval(change,5000);
Ahh, to explain images.reverse()[0];, it reverses the array order and we always take out the 0 indexed key. Quite nice
The reason your code didn't work is you comparison is in appropriate. Where you have:
var images=[]
images[0]="smile.gif";
images[1]="frown.gif";
var myMood= document.getElementById("mood");
function change(){
if (myMood == images[0]) {
then myMood is a DOM element and images[0] is a string. Those two will never almost never be equal (unless the DOM element's toString methods produces exactly the same string).
You probably meant:
if (myMood.src == images[0]) {
so that you compare two strings that are more likely to be the same.
Inside an HTML I have a script:
<script src="info.js" type="text/javascript"></script>
Inside info.js there is a function, lets say getInfo(key) which returns info.
How can I call a function located inside info.js from my html, so that I can use this variable inside the whole html document?
w3schools does not explain how to do this
Something like this:
var global; // global var
// js code...
// function
function myFunc(){
global = getInfo(key);
}
This might help you understand what's going on.
Let's say the code below is located inside info.js
function getSomething(){
return "something";
}
Now your HTML might look like this
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="info.js" type="text/javascript"></script>
</head>
<body>
<span id="mySpan"></span>
<script>
var myText = getSomething();
$('#mySpan').text(myText); //this uses jQuery so you'll need to include that
</script>
</body>
</html>
If I got you correctly, that you want to use that info in all your html file, here's my answer. Let me know if it helps or if you meant anything else, I'll think accordingly.
<html>
<head>
<script src="info.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var info = getInfo(key);
$('div.myinfo').html(info);
});
</script>
</head>
<div class="myinfo"></div>
</html>
You have the desired information in a javascript var and you can assign it as value to a input/select element or copy it as html to a div/td/p/h1 etc.
Just use it like this:
var info = getInfo(key);
As long as your calling script follows the method in info.js (and assuming the function is not private) it should be accessible in any script
I am using tinyMCE library to edit HTML which returns something like this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
... Your HTML ...
</body>
</html>
I would like to strip out the doctype, html, head and body tags so it will be
... Your HTML ...
Here is the function I am using.
function stripHTML(html) {
return str.replace(/<(\/?|\!?)(DOCTYPE html|html|head|body)>/, "");
}
This only removes <!DOCTYPE html> from the string.
If there's an even easier way to do this, please point me in the right direction. Any ideas?
function stripHTML(html) {
return str.replace(/<(\/?|\!?)(DOCTYPE html|html|head|body)>/g, "");
}
You need a global modifier to get all cases
http://regex101.com/r/aA1vL0
Use this:
return str.replace(/<(\/?|\!?)(DOCTYPE html|html|head|body)>/g, "");
You don't have the g global flag so it's only replacing the first element it matched.
Problem that \r\n symbols are not match your expression.
You can use this expression to strip all you want:
function stripHTML(html) {
return str.replace(/(<![\w\W]*<body>|(<\/body>\W*<\/html>))/gmi, "");
}
I've an html page with JavaScript. Inside it I load another JavaScript with the tag :
<script type="text/javascript" charset="utf-8" src="/../myfile.js" ></script>
Furthermore my html page is encoded with the following tag :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Then, inside a function I get a string defined in "myfile.js" and want to compare it to a string coming from my main page. I tried indexOf, ==, search, match... but the assertion will always be false. I alerted my strings and they were showed as equal (case sensitive).
Anybody has an idea of why my test doesn't work?
Edit : my code looks like the following.
window.lang= new myobject(); // this object is defined in myfile.js
var mystring1 = window.lang.attr1['mykey'];
var mystring2 = $("#mydivid").html();
alert(mystring1+":"+mystring2); // this shows 2 equal strings
/* Here I wanted to test if the 2 strings are equals and tried "==", mystring1.indexOf(mystring2), match, search, ... */
And in file.js :
myobject.prototype.attr1 = {
'mykey': 'mystring1value'
}
You need "utf-8" ,not "uft-8":
<script type="text/javascript" charset="utf-8" src="/../myfile.js" ></script>
Sample1.html
New Document
</HEAD>
<script type="text/javascript" charset="utf-8" src="Sample2.js" ></script>
<script>
function compare(){
j="test";
alert(i==j);
}
</script>
<BODY onload="compare();">
</BODY>
</HTML>
Sample2.js
var i="test";
Please try this its works fine for me
Finaly the issue came from a jquery plugin (jsTree) that added some "\n" or "\r" at the end of my strings. Sorry for disturbance and thanks for help !
Cheers,
Ricola3D
Is there any way to fix the error that a JavaScript & causes in w3 validation? The problem is that i have to use && in a if statement and these two &&'s causes errors in w3 validation.
EDIT:
Same problem with "<" and ">".
There are a few things you can do.
You can enclose it within HTML comments:
<script type="text/javascript">
<!--
if (foo && bar) ...
//-->
</script>
You can enclose it in a CDATA section:
<script type="text/javascript">
// <![CDATA[
if (foo && bar) ...
// ]]>
</script>
You can include in in a file instead:
<script src="foobar.js" type="text/javascript"></script>
The primary answer is: Use JavaScript files for JavaScript, not HTML files, and use the src attribute of script tags. (Combine all your JS into one file, minimize, gzip, etc. for performance.)
But, you can embed JavaScript in HTML if absolutely necessary. Use a valid, modern DOCTYPE and you don't have to resort to comment tags and CDATA sections.
Valid HTML5 example:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Example</title>
<script type='text/javascript'>
function foo() {
var a = 1, b = 2;
if (a && b) {
alert("Both");
}
if (a < b) {
alert("a < b");
}
if (a > b) {
alert("a > b");
}
}
</script>
</head>
<body>
<p>Hi there</p>
</body>
</html>
That will also validate as HTML4 strict if you change the doctype to
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Note that in both cases, you need to be careful about end tags in your script --
This causes the problem:
<script type='text/javascript'>
alert("</td>");
</script>
This solves the problem by prefacing the slash with a backslash (or you can break the end tag up into separate string literals):
<script type='text/javascript'>
alert("<\/td>");
// -or-
alert("<" + "/td>");
</script>
But again, the basic answer is: Don't embed JavaScript within HTML files when you can avoid it, use JavaScript files for that.
Based on your description, I suspect that you're talking about script that's inside an event property in an HTML tag (such as onclick). In that event, the script code needs to be HTML encoded. Elijah hit the nail on the head there.
For example:
<input type="submit" onclick="if(somevar && othervar) callFunc("clicked");">
You do not need to do that inside a <script></script> block.
Escape & with &, < with <, and > with >.