escaped char generating "missing ) after argument list" error? - javascript

This little line of code here is from a shopping cart:
Add To Cart
Firebug's console shows: "missing ) after argument list". Clearly the ')' isn't missing! But I suspect it has something to do with the escaped char
'
since the other similarly formatted links without apostrophes in the name= argument are working fine.
Thoughts?

onclick="simpleCart.add( 'name=The Devil's Sneakers'...
Is an HTML attribute with the apostrophe escaped at an HTML level. It is exactly the same as saying:
onclick="simpleCart.add( 'name=The Devil's Sneakers'...
with the obvious problem with the string closing too early. HTML-escaping doesn't help you because the HTML-escape is only needed to encode the characters that are special to HTML. That would include a double-quote character but not a single quote, since you've used double-quotes to delimit the attribute value.
The apostrophe isn't special to HTML here, but it is to JavaScript. You need to use JavaScript string literal escaping, and in that kind of escaping you need backslashes:
onclick="simpleCart.add( 'name=The Devil\'s Sneakers'...
Either way, it's clear that escapes inside other escapes are really confusing and this is another good reason not to use inline event handler attributes. Instead, in plain JavaScript:
<button type="button" id="foo">Add to cart</button>
<script type="text/javascript">
document.getElementById('foo').onclick = function() {
simpleCart.add("name=The Devil's Sneakers", 'price=10', 'shipping=0', 'quantity=1');
};
</script>
(I used a button because what you've got isn't a link that goes anywhere. You can style it to look like a link instead of a button if you prefer, but it's better not to have a link if none of the normal affordances of links, like middle-click or right-click-bookmark make any sense.)

Escape apostrophes with \x27. This code should work:
Add To Cart

Try \' instead of '
It works well.
<html>
<body>
Add To Cart
Add To Cart
</body>
</html>

Related

JavaScript RegExp replace HTML comments

I'm searching a way to replace all html comments from a string like browser does. (multilined and unclosed)
For example, I actually use /(<\!--[\s\S]*?-->)/gim but if the html comment is unclosed, it does not replace it.
Normally, if the comment tag is not closed, comment tag gets everything after open tag...
Is there a way to adapt the regexp (or any other regexp) to do the stuff ? (in JavaScript)
This will mark all comments also the one without end tag: <!-- some text -->
<!--[\s\S]*?(?:-->|$)
This will mark all comments also the one without end tag: <!-- some text //-->
<!--[\s\S]*?(?://-->|$)
This will mark everything from the first <!-- to the very end of the file
<!--[\s\S]*?(?:$) and regex set to `^$ don't match at line breaks`
This will mark everything from the first <!-- to the end of the line
<!--.*
I must agree that using regex like this is not good practice and you shouldn't do it... here's why.
Buuuut, as a matter of understanding regex better, you can make something optional like this:
/(<\!--[\s\S]*?(?:-->)?)/gim
I wrapped --> in parenthesis to group it together
I put a ? after that group to make it optional
(not necessary) I put ?: inside of the group to keep the regex engine from saving a back reference... it's a performance nuance.
Thanks to #Andie2302 for the help.
This regexp /<!--[\s\S]*?(?:-->|$)/gi work find.
Do not use the flag m!

passing json as parameter in javascript with space in content

The following is working when the content is no space
<a onclick=fbShareDialog("{\"name\":\"aaaaaaa\"}">
but if there is a space between
<a onclick=fbShareDialog("{\"name\":\"bbbbb bbbbb\"}">
it throws Uncaught SyntaxError:unexpected token illegal
as i think all the content is in quotation , why not works?
thanks in advance
You need to make sure you escape the space and put everything within double quotes.
I can't test it out now but try something like.
Also, it may be a better idea if you didn't write this inline and wrote a function to catch the click event instead.
You're implying a quoteless-attribute, which is really bad form in HTML but unfortunately still allowed due to HTML's sordid history. The attribute is 'onclick', which actually includes fbShareDialog, but that isn't in quotes.
Try:
<a onclick='fbShareDialog({"name":"bbbbb bbbbb"})'>
Or better yet, bind your click events in a .js file for your app, not onclicks in the html.

Put a quote in a HTML attribute

example,
<span class="hotspot"
onmouseover="tooltip.show('<table id=\"test\"><tr><th>123</th></tr><tr><th>123</th></tr></table>');"
onmouseout="tooltip.hide();">porttitor orci</span>
in here, i'm trying to add id attribute in table tag, but i already used "" and '', so something is messed up and not working.
Any good solution?
<span class="hotspot"
onmouseover="tooltip.show('<table id="test"><tr><th>123</th></tr><tr><th>123</th></tr></table>');"
onmouseout="tooltip.hide();">porttitor orci</span>
JSFiddle
In an attribute value that is delimited by Ascii quotation marks ("), you can present the Ascii quotation mark itself using the reference ".
However, there is usually a better way. In the given case, you can simply omit the quotation marks, since id=test works just fine (unless you are using XHTML served with an XML content type, and most probably you aren’t).

Declaring a parameter in html and receiving it in Javascript

My js function goes like this:
function tester (message) {
alert(message);
}
And in the markup I have:
Link
But it doesn't work. Can someone please tell me why?
text to show is not a string unless you wrap it with quotes, either single or double.
Like this:
Link
Notice that you can't use the same kind of quote for both the Javascript code and the HTML.
Your code is probably giving errors. Check the console when the code doesn't do what you expect it to.
You are almost there. All you have to do is wrap text to show in quotes, like this:
Link
What's happening here is that you have an <a> tag with two attributes, href and onclick. In the href you write the URL that the link points to. In the onclick attribute, you write Javascript. The Javascript here is:
tester('text to show');
which runs a function called tester, passing it the string 'text to show'. Your original code had Javascript like this:
tester(text to show);
which results in a Syntax Error. As an aside, if you had Javascript like this:
tester(text);
it would have looked for a variable named text, and if this variable was defined, you would get an alert with that text.
One more thing: when you include text inside HTML attributes, like you did here, you should be careful not to use the same quotes you used to enclose the attribute. That is why we used single quotes. Suppose you wanted to use double-quotes or other fancy characters, such as the © symbol ... then you replace them with their "HTML-escaped" value, in this case ©. Click here for some common values.

Iavascript \" \' I'm confused

I'm busy on little JavaScript item, but I got confused by the next rule:
<img src="images/icons/collapse.gif"><br/>'
It doesn't take the onclick remove map action. I don't know what I have to put between the () to make I can put text there. Does somebody have the solution?
Try just :
onclick="removeMap('test')"
if test is a variable:
onclick="removeMap(test)"
The \" is to scape the closing double quote of the onclick event. In this case I don't see any use in your calling. When used event functions in the html tags just like your onclick event, as html uses double quotes for the attributes you need to use the single quotes in your inline javascript functions.
By the way, have you checked about JQuery JavaScript Library. As you are starting coding javascript it's good to know your options.
Or just stop writing javascript inside of your HTML tags, just call functions and put your code in your functions... far much readeable !
How about:
<img src="images/icons/collapse.gif"><br/>'
You should use an editor that properly highlights strings (and what is going on should be obvious)
Escaped quotes don't have any special meaning in HTML. Instead, the browser will see your line as:
<a href="#"
onclick="removeMap('+ \"
test\
" +')"
style="float:right; margin-right: 30px;"
>
Where the onclick would be broken Javascript, and there would be two attributes in the tag (test\ and " +')") that it doesn't recognize and doesn't know what to do with.

Categories

Resources