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
Related
I am using this code to display summernote editor on my page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bootstrap4</title>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote-lite.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote-lite.js"></script>
.
.
.
<textarea id="summernote" name="summernote">#Model.InviteEmailBody</textarea>
<script>
$('#summernote').summernote({
tabsize: 4,
height: 220
});
$('#summernote').on('summernote.blur', function () {
$('#summernote').html($('#summernote').summernote('code'));
});
$('#summernote').html(escape($('#summernote').summernote('code')));
</script>
Then saving to database
string str = Request.Form["summernote"];
string htmlEncoded = WebUtility.HtmlEncode(str);
roleToUpdate.InviteEmailBody = htmlEncoded;
Everything is fine when I actually edit the content, but when i don't, the value I fet in str variable has a lot of
%3Cdiv%3EHello%20
I am not sure if the way I implemented summernote is correct or how would I fix this problem.. if anybody can help I would appreciate it
This garbage is produced by
$('#summernote').html(escape($('#summernote').summernote('code')));
If you want to have your textarea cleaned up (or predifed with some text), you may use the following syntax. The second argument is a string value you want to set:
$('#summernote').summernote('code', '')
$('#summernote').html(escape($('#summernote').summernote('code', '<b>some</b>')));
I have that link http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella that shows on the browser a json with all the values, so I need to pick only the wind, and the temp, but I don't know how to extract it, how to do that?
I thougt in add a callback after "Andorra+la+Vella"&callback=myFunction
And develop a code on javascript to get the values that I need, but I do it and something goes wrong, that's a simple test to try this little application and then i add it to my web:
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Test tiempo andorra</title>
<script type="text/javascript">
function showTime(obj) {
var dadIp=document.getElementById('time');
dadIp.innerHTML+=obj.sunset.wind.speed;
}
</script>
</head>
<body>
<div id="time">
<h1>Test of Time in Andorra</h1>
</div>
<script type="text/javascript" src="http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella&callback=showTime"></script>
</body>
</html>
Look at your JavaScript error console.
Uncaught TypeError: Cannot read property 'wind' of undefined
Then look at the JSON source.
obj.sunset doesn't exist. obj.sys.sunset does.
obj.sunset.wind doesn't exist. obj.wind does.
See a live example when the correct object paths are used.
This way for example:
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" )
;
In case if you need parse DateTime I advise to use moment.js library.
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)
I have this problem when i use $('#id').val().length; it returns 2 when I use characters like æ, ø and å.
Can someone tell me why and how I can get it to work like ( one ) char?
I suspect something else is going on here and it is not an issue of encoding.
I refuse to believe this is a jQuery issue (see http://jsfiddle.net/KLzYf/ for my jsutification).
The following raw HTML will report back "1":
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<html>
<body>
<input type="text" value="æ" id="test"/>
</body>
<script type="text/javascript">
alert(document.getElementById("test").value.length);
</script>
</html>
I'd be interested to see some of the HTML/other code. And to have a few tests, for instance, what do the following give you
alert("æ".length); //=1?
alert('"' + $('#id').val() + '"'); //are there any spaces/other chars?
Also, if you view-source on the HTML, what do the contents of your input look like.
try this:
http://jsfiddle.net/Innuendo108/GXwGG/
There are 2 characters and it says length=2
I think you used extra space either or any one side of that character.
<p id="t">æ</p>
$('#t').text().length
This work properly.
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 >.