JS 'Unterminated string constant' error - javascript

On my view I have this:
#if(ViewBag.Test == true)
{
<script>
window.alert("test")
</script>
}
Here is a picture:
As you can see, the red wave-line is saying that it is an Unterminated string constant.
How do I fix this?

It may be choking on the closing script tag.
Are you able to do something like the following:
string script = "<scr" + "ipt>window.alert('test');</scr" + "ipt>";
Response.Write(script);

Related

Representing # symbol in javascript string

I'm trying to access the position of the # symbol and using the following JS...
<script>
$(document).ready(function () {
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("#");
});
});
</script>
However this produces a Parse Error:
"");" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.
Do I need an escape character here? When I replace the # with some other character, say a letter. It does not crash. This code works...
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("w");
e.preventDefault();
Assuming you are trying to execute the exact snippet above, the error is telling you that your code is not valid. Specifically, you are missing a closing curly brace (}) and a closing parenthesis ())
Here is what it should look like:
$("#submitButton").click(function(e) {
var at = $('#email').val().lastIndexOf("#");
console.log('at', at);
e.preventDefault();
}); // <-- Closing } and )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="submitButton">Click Me</button>
<input id="email" type="text" value="something#something.com" />
As far as the # symbol goes, you do not need to escape it in this scenario.
EDIT
The error your getting smells a lot like you're using this in an ASP.NET controller. You might just be including your JS wrong. Try including it by doing it this way:
#scripts {
<script>
$(document).ready(function () {
$("#submitButton").click(function (e) {
var at = $('#email').val().lastIndexOf("#");
});
});
</script>
}
I found it! The # symbol needs an escape character...another # symbol. This works...
var at = $('#email').val().lastIndexOf("##");

Getting string out of regex () does not behave as expected

I am currently working on a project where I need to match specific html tags and replace them by some others.
I am using Javascript in order to do so, and the code looks like :
// html to update
html = '<div class="page-embed"><article><iframe src="https://alink" width="100%"></iframe></article></div>';
// regex that will match the specific "<div class="page-embed">...<iframe src="https://alink"></iframe>...</div>
const regexIframeInsideDiv = /<\s*div\s*class="page-embed"[^>]*>.*?<\s*iframe[^>]*\s*src="(.*?)"\s*><\s*\/\s*iframe\s*>.*?<\s*\/\s*div\s*>/g;
html = html.replace(regexIframeInsideDiv, (_match, src) => {
console.log(src);
return `<oembed>${src}</oembed>`;
});
I use the () tool to get what is inside the source attribute as follow :
src="(.*?)"
Here is the problem :
If I run the code, the console will log :
https://alink" width="100%
where it should log :
https://alink
I might be missing something, like escape string or an error anything else.. but I don't know what.
Here is the expected behaviour :https://regexr.com/4tbj6
Thank you !
In your regex, on the part you are matching src, it's not \s* but \s.*
src="(.*?)"\s.*>
// html to update
html = '<div class="page-embed"><article><iframe src="https://alink" width="100%"></iframe></article></div>';
// regex that will match the specific "<div class="page-embed">...<iframe src="https://alink"></iframe>...</div>
const regexIframeInsideDiv = /<\s*div\s*class="page-embed"[^>]*>.*?<\s*iframe[^>]*\s*src="(.*?)"\s.*><\s*\/\s*iframe\s*>.*?<\s*\/\s*div\s*>/g;
html = html.replace(regexIframeInsideDiv, (_match, src) => {
console.log(src);
return `<oembed>${src}</oembed>`;
});
Try this RegEx:
(?<=(<div class="page-embed".+iframe src="))(.*?)(?=")
Which searches for a String between src=" and the next " in a div with your class and an iframe.

Unexpected token < From a Style Tag

I keep getting "Unexpected token <" because of this script:
<script type='text/javascript'>
$(document).ready(function(){
if (window.location.pathname + window.location.search = '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write (<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>);
}
});
</script>
I don't understand why it's throwing that error. I have been researching for about 2 hours now. I tried adding CDATA tags, I tried using entity names instead of characters, I made sure there was no whitespace within the document.write, etc, etc. Why won't it work? I thought document.write supported HTML entities?
EDIT: I changed the = operator to == . I also added single quotes, but then when I submitted to Blogger I got the XML error: "The content of elements must consist of well-formed character data or markup" so I changed the HTML Characters to HTML Names and resubmitted. I am still getting the "unexpected token" < error...
UPDATE I have updated the script to this, but still get the exact same error:
<script type='text/javascript'>
<![CDATA[
$(document).ready(function(){
if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write ('<style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>');
}
});
]]>
</script>
At least you have to add a single quote around your string ...
<script type='text/javascript'>
$(document).ready(function () {
if ((window.location.pathname + window.location.search) === '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
// add the style to your head
$('head').append(String.fromCharCode(60) + 'style type="text/css">#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }' + String.fromCharCode(60) + '/style>');
// or decide to individually show the divs with jquery selectors
$('div#HTML25').css('display', 'block');
}
});
</script>
Try this:
<script type='text/javascript'>
$(document).ready(function(){
if (window.location.pathname + window.location.search == '/search/?q=label:Web-Design|label:Graphic-Design|label:Identity-Design|label:Brand-Design') {
document.write("<style type='text/css'>#HTML25, #HTML23, #HTML22, #HTML24 { display:block; }</style>");
}
});

Remove all scripts with javascript regex

I am trying to remove all scripts tags with content from the string of the type below with regex in javascript.
But I am still getting as output:
");</script>
when it should be an empty string.
The code is:
var BG = '<script type="text/javascript">document.write("<script type=\"text\/javascript\" src=\"http:\/\/site;js=y;target=_blank;time="+ (window.emediate_time ? window.emediate_time : window.emediate_time = new Date().getTime()) +"1053997930;"><\/script>");</script><script type="text/javascript" src="some?cre=mu;js=y;target=_blank"></script>';
BG = BG.replace(/<\s*script.*?>.*?(<\s*\/script.*?>|$)/ig,'');
Could you please tell me what's wrong and how to fix it. Thanks.
Try this:
(/<.*?script.*?>.*?<\/.*?script.*?>/igm, '')
or
(/<script.*?>.*?<\/script>/igm, '')
(you need 'm' switch to search multi-line)

Why am i getting this missing } after property list error? :(

I know this is something easy but I just can't see it. Can anyone tell me why I am getting the error "missing } after property list" for the following code:
<script type="text/javascript">
$(".single_image").live("click", function() {
jwplayer().load({
file: 'utubeurl'
});
});​
</script>
the whole of the code is shown below:
$(xml).find('item').each(function(i){
var current=$(this);
var ttl=current.find('title:first').text();
var thum=current.find('thumbnail:first').text();
var cat=current.find('category:first').text().split(',');
var desc = current.find('description:first').text();
var utubeurl = current.find('youtubeurl:first').text();
var fbshareurl = current.find('facebookshareurl:first').text();
var twturl = current.find('twitterurl:first').text();
var nbcurl = current.find('nbcsiteurl:first').text();
var item = {
title:ttl,
thumbnail:thum,
category:cat,
description:desc,
youtubeurl:utubeurl,
facebookshareurl:fbshareurl,
twitterurl:twturl,
nbcsiteurl:nbcurl,
obj:$('<div class="'+options.itemClass+'"><a id="'+parentId+'" class="single_image" title="'+desc+'"><script type="text/javascript"> $(".single_image").live("click",function(){ jwplayer().load({file:'+utubeurl+'}); }); </script><img src="'+thum+'" /></a><div class="show_lightbox_title"><strong>'+ttl+'</strong></div><ul id="social"><li><iframe src="'+fbshareurl+'" class="iframe_style" scrolling="no" frameborder="0" allowtransparency="true"/></li><li><a class="twtbtn" href="'+twturl+'" target="_blank"><img src="images/twitter_btn.gif"></a></li><a class="nbcbtn" href="'+nbcurl+'" target="_blank"><img src="images/showPages_btn.gif"></a></div>')
};
shows.push(item);
});
You need to quote your property value, here:
obj:$('<div class="'+options.itemClass+'"><a id="'+parentId+'" class="single_image" title="'+desc+'"><script type="text/javascript"> $(".single_image").live("click",function(){ jwplayer().load({file:'+utubeurl+'}); }); </script><img src="'+thum+'" /></a><div class="show_lightbox_title"><strong>'+ttl+'</strong></div><ul id="social"><li><iframe src="'+fbshareurl+'" class="iframe_style" scrolling="no" frameborder="0" allowtransparency="true"/></li><li><a class="twtbtn" href="'+twturl+'" target="_blank"><img src="images/twitter_btn.gif"></a></li><a class="nbcbtn" href="'+nbcurl+'" target="_blank"><img src="images/showPages_btn.gif"></a></div>')
...this:
'... jwplayer().load({file:'+utubeurl+'}); ...'
...needs to be:
'... jwplayer().load({file:"'+utubeurl+'"}); ...'
...note the extra quotes. Not sure if adding those quotes will break your looooooooong (difficult to read/support) string, you might need to escape them. But you get the idea?
Cheers
Try putting the file property under quote marks , like so:
function () {
'file' : 'utubeurl'
}
--EDIT:
My bad , forget it , I was confusing with json, jquery and maybe some other j out there, you're definning a property , no need to make the name of the memory slot a string .
When I copy and paste that block of code, I see an extra character trailing the second closing });
Removing that executes fine in console for me, so if that is not the source of the error I would look elsewhere on the page.
Is the page publicly accessible?
Solved the problem by passing a variable through href and then passing it into the command to play the url.
var item = {
title:ttl,
thumbnail:thum,
category:cat,
description:desc,
youtubeurl:utubeurl,
facebookshareurl:fbshareurl,
twitterurl:twturl,
nbcsiteurl:nbcurl,
obj:$('<div class="'+options.itemClass+'"><img src="'+thum+'" /><div class="show_lightbox_title"><strong>'+ttl+'</strong></div><ul id="social"><li><iframe src="'+fbshareurl+'" class="iframe_style" scrolling="no" frameborder="0" allowtransparency="true"/></li><li><a class="twtbtn" href="'+twturl+'" target="_blank"><img src="images/twitter_btn.gif"></a></li><a class="nbcbtn" href="'+nbcurl+'" target="_blank"><img src="images/showPages_btn.gif"></a></div>')
};
shows.push(item);
});
setSetter();
}
});
}
utubeurlParser = function(url){
jwplayer().load({file: [url]});}

Categories