Unexpected token < From a Style Tag - javascript

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>");
}
});

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("##");

Adding a jQuery code to a PHP string

I want to add a jQuery code to a PHP string
Here is the PHP code
$my_code ='
<script src="js/jquery.form.js"></script>
<script>
$(document).ready(function()
{
$("#my_form").on("submit", function(e)
{
e.preventDefault();
$("#myButton").attr("disabled", "");
$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
$(this).ajaxSubmit({
target: "# my-output ",
success: afterSuccess
});
});
});
function afterSuccess()
{
$("#myButton").removeAttr("disabled");
}
</script>
‘;
Issue I’m having in this code line
$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
I even try change the single quote to double quotes and when I do code stopped working. It works fine if I don’t have a div inside above code line.
Ex:
$("#my-output").html(“My message”);
Appreciate your answers.
You can use single quotes inside the string, however you will need to \' escape them:
'$("#my-output").html(\'<div class="alert alert-info" role="alert">My message</div>\');'
You should use Heredoc Syntax to avoid quote issues.

How to get value of dom element whose id having dot and dollar sign

I am facing problem with getting value of an element whose id contains special character as . and $ sign.
my id to element is "XS_19MAY2016_012720_311.04$_cal" I am using syntax of jQuery as:
$("#"+xyz+"_cal").val() where xyz is variable having above id.
I am getting error as:
Error: Syntax error, unrecognized expression: #XS_19MAY2016_012720_311.04$_cal.
What I doing wrong or what I need to do to correct it.
Just escape the characters:
var foo = 'XS_19MAY2016_012720_311.04$';
$('#' + foo.replace(/[.$]/g, '\\$&') + '_cal').text('foo')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="XS_19MAY2016_012720_311.04$_cal"></div>
You can use this way to do that (without symbol escaping).
$(function() {
var
id = "XS_19MAY2016_012720_311.04$_cal",
text;
text = $('[id="' + id +'"]').text();
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="XS_19MAY2016_012720_311.04$_cal">Yay!</div>
You need to tell jquery that string is absolutely an 'ID', not a class.
HTML ID attribute can't includes space, and must have at least one character. But for compatibility, better to avoid '_', '.'
Here using the Javascript builtin method, and turn the DOM node to a Jquery object. It looks redundant, but it is steady and easy to read.
$(function() {
var xyz = 'XS_19MAY2016_012720_311.04$';
e = $(document.getElementById(xyz + '_cal'));
console.log(e.text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="XS_19MAY2016_012720_311.04$_cal">
hello
</div>

JS 'Unterminated string constant' error

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);

Get Item Count for a Specific View

Is there a piece of jQuery or JavaScript that I can run that will allow me to create a Content Editor Web Part that has "This is the number of list items: XYZed."
I've tried this but it is not working:
http://www.martinhatch.com/2010/09/how-to-achieve-count-on-large.html
The one I tried was the 4'th attempt. If someone could help me that would be fantastic.
try these solutions/posts in the given order.
https://sharepoint.stackexchange.com/questions/5477/getting-a-count-of-list-items-in-a-list-via-ecmascript
https://sharepoint.stackexchange.com/questions/18050/how-do-i-get-the-number-of-items-in-a-list
http://www.codeproject.com/Questions/266281/jQUERY-TO-GET-COUNT-OF-TOTAL-ITEMS-EXISTED-IN-SP-L
It worked for me. All I had to do was create the grouped view and put the url into the code. Tho I used quotes
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//////////
// you will need to change this URL
var url = "http://sppos/Lists/Statistics/Grouped.aspx";
//////////
var groupings = [];
$.get(url, function(data) {
$(data).find("tbody[id^=titl][groupString] > tr > td").each(
function(index, value){
groupings.push($(this).text());
}
);
$("#placeholder").append("<ul></ul>");
$.each(groupings, function(index, value){
$("#placeholder ul").append("<li>" + value + "</li>")
});
});
});
</script>
<div id="placeholder"></div>
After testing, no quotes doesn't work, which may be the cause of some grief

Categories