Javascript bookmarklet...I think browser is replacing characters - javascript

I have this lovely javascript bookmarklet...
javascript:var nam="blablabla&name"; var els=document.getElementsByName(nam); if(els.length == 0) alert(nam); else els[0].click();
when I put that in I get this result:
blablabla)name
so basically I think the browser (or something) is automatically replacing the & with the ). I need it to not do that. I want whatever is between the "" after nam= to be sent exactly as is. How can I do that?

I think the problem is not the javascript-variable, it's the name-attribute. If you have there an entity it will be translated to the corresponding character. So if you really have a name like:
<a name="blablabla&name">
you'll have to set the variable to
var nam='blablabla&name';

You can't use " " while doing a bookmarklet, because those will close the attribute try to use ' ' instead

Related

what is proper syntax for regex to get specific text from GTM attribute.response

I used Google Tag Manager to create a custom data level variable to get the content of an ajax form. The result is in the attributes.response that looks like:
response:"{\"current_url\":\"https:\\/\\/domainname.com\\/ +
"manufacturer\\/category\\/model-number\\/\",\"h" +
"tml"\":{\"cart_status_528\":\"\\n <div id=\\\"s" +
...
"<a href=\\\"https:\\/\\/domainname.com\\/manufacturer" +
"-name\\/long-store-category-name\\/model-number-x\\/\\" +
"\" class=\\\"ty-product-notification__product-name\\\"" +
">PRODUCT-NAME THAT I WANT<\\/a>\\n " +
...
" <p><\\n more escaped html content +
}"
I am trying to extract/parse the attribute.response to retrieve the PRODUCT-NAME text. I have tried the following which matches in regexr. But, GTM keeps complaining there is an error in my javascript at the double quote symbol. What am I missing? Or is there a cleaner way to retrieve the text? Thanks
function() {
var regex = (?<=product-name(.|\n)*">)(.*)(?=<\\\\\/a);
var attributesResponse = {{attributes.response}};
if(regex.test{{attributesResponse}}
var ProductAddedToCart = regex.exec(attributesResponse)[1];
return ProductAddedToCart;
}
return false;
}
First of all, please read the top answer here: RegEx match open tags except XHTML self-contained tags
Secondly, your JS has many problems. Even the SO code highlighter indicates it. See some examples of how regex is used in JS.
The proper way to solve your task, however, would be adding a dataLayer push with the proper response details neatly stored in a dataLayer object. You would normally ask your front-end developers to add a push in their response callback. It should be trivial for them to tackle. You can read more on DL here.

Multiple attribute on href using onclick

I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print

How to get bean values in jsp scriplets and store in a javascript variable?

<bean:write name="<%=(String) currentItr.next()%>" property="<%=(String) currentItr.next()%>"/>
The above code prints the values aka : Nicholas...$100000...45....Actor
How can i assgin Nicholas to a javascript variable?
I tried
var audition = <%=(String) currentItr.next()%>.<%=(String) currentItr.next()%>;
and it prints out the column header like so : Name...Salary...Age...Occupation
what am i doing wrong?
You should try this , if you are assigning string value then use ' ' or " " . For Number value what you wrote was correct .
var audition = "<%=(String) currentItr.next()%>.<%=(String) currentItr.next()%>";
#LandLane Why don't you set this scriptlet value into hidden text box, and call the value from JS. How do you calling the javascript from java? If you are using logic:iterate tag means there may be n number of values like nicholas. Post your code, unless nobody can't diagnose it.

Can I return the value of document.getelementbyid inside a document.setcookie?

<input type=button value='Do Stuff' onClick='document.cookie = "Note_<% Response.Write(arrTest(0,i)) %> = ' + (Document.getElementById("StuffTest<% Response.Write(arrTest(0,i)) %>").value) + '"'>
What I'm trying to do here is when the user hits the button, I want a cookie created with the values: Note_(value of arrTest) = (Value of the text box).
The code below works just fine:
<input type=button value='Do Stuff' onClick='document.cookie = "Note_<% Response.Write(arrTest(0,i)) %> = Awesome!"'>
It creates a cookie with the value of: Note_(value of arrTest) = Awesome!
What have I done wrong to get the value of my text box into the cookie? I'm assuming it has to do with all those confusing single/double quotes, so I think I've just got typo-blindness, but part of me thinks I can't actually do what I'm trying to do here.
Any thoughts?
document.getElementById has document in all lower case. Your code example uses Document (with a capital D), which will fail.
Separately, your quotes such in the onClick attribute are not quite right. It's best to avoid putting any significant code in those attributes. Instead, define and call a function, like this:
<input type=button
value='Do Stuff'
onClick='setCookie("<% Response.Write(arrTest(0,i)) %>);'>
...where the function (inside a <script> tag or in a file referenced by one) looks like this:
function setCookie(name) {
document.cookie = "Name_" + name + "=" + document.getElementById("StuffTest" + name).value;
}
Depending on the contents of arrTest(0,i), you might need to HTML-encode it as you output it, since you're outputting it to HTML (yes, the code inside an onXYZ attribute is HTML, just like any other attribute's value, so for instance < would need to be <).

How might I replace a portion of a link's href attribute with a variable value?

I have the following code, written using jQuery:
var strval = '22'
$("#quicknote").attr("href",
"javascript:location.href=/'http://site.com/notes/add?projects=/'+ strval")
Which results in the following attribute:
<a href="javascript:location.href='http://site.com/notes/add?projects='+'strval'"
id="quicknote">
But, what I want is:
<a href="javascript:location.href='http://site.com/notes/add?projects='+'22'"
id="quicknote">
Any jQuery wizards know how I might achieve this result?
Try:
var strval = "22";
$("#quicknote").attr("href",
"javascript:location.href='http://site.com/notes/add?projects=" + strval + "'");
Note the position and type of quotes.
On a side note, I'm not exactly sure why you wouldn't to do this instead:
var strval = "22";
$("#quicknote").attr("href",
"http://site.com/notes/add?projects=" + strval + "'");
ie theres no need for Javascript in your example.
Lastly, since you are using jQuery anyway I wouldn't put Javascript in the href like this either. Instead add a click() handler:
$("#quicknote").click(function() {
window.location = "http://site.com/notes/add?projects=22";
return false;
});
A JQUERY wizard tells you, that it is possible, but it has just nothing to do with jQuery, and that your answer is:
You have the quote wrong!
And there is no reason to vote me down, because I answered the initial question in very, very, very professional style.

Categories

Resources