I am trying to insert a tag into the page. but inside that a tag, it has both single quote and double quote, so i try put that part into an variable Var content. Can someone tell me why this var content is not showing on onclick after code runs?
var content = "javascript:window.open('https://ds.contactatonce.com/ds/p10819departmentselector.htm?OriginationUrl='+encodeURIComponent(location.href),'','resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,height=520,width=450');return false;"
$('<a style ="float: left; margin-top: 20px" href="#" onclick=" + content + " ><img onerror="this.height=0;this.width=0;" border="0" alt="Click to instant message Representative Now!" title="" src="https://dm5.contactatonce.com/getagentstatusimage.aspx?MerchantId=270075&ProviderId=10819&PlacementId=10"></a>').insertBefore('.header-contact-phone');
it was like this before but it keep showing errors
$('<a style ="float: left; margin-top: 20px" href="#" onclick="javascript:window.open('https://ds.contactatonce.com/ds/p10819departmentselector.htm?OriginationUrl='+encodeURIComponent(location.href),'','resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,height=520,width=450');return false;" ><img onerror="this.height=0;this.width=0;" border="0" alt="Click to instant message !" title="Click to instant message a Now!" src="https://dm5.contactatonce.com/getagentstatusimage.aspx?MerchantId=270075&ProviderId=10819&PlacementId=10"></a>').insertBefore('.header-contact-phone');
Use the escape character \ before the quotes
like this:
var y = "We are the so-called \"Vikings\" from the north.";
see this links:
https://www.w3schools.com/js/js_strings.asp
https://www.w3schools.com/js/tryit.asp?filename=tryjs_strings_escape
Related
I can't hide my button using javascript, my code is this. (I used an image as a button)
<img alt="" src="images/search-icon.png" width="16" height="16" style="display: inline;height:16px;cursor:pointer; width: 16px;" onclick="ViewLookUpPayment('<%=btnShowCorpCode.ClientID %>');" />
Here is the code in my javascript.
function ViewLookUpPayment(bShow)
{
var button1 = document.getElementById(bShow);
button1.style.visibility = "visible";
}
Every time i click on the image this runtime error pops up
I can't understand why is there a runtime error, Can anyone help me on this one?
Did you set 'id' attribute to image tag?
May be "document.getElementById(bShow)" returns 'undefined'. Try checking what "var button1" is.(example: console.log(button1))
try this
<img alt="" src="images/search-icon.png"
width="16" height="16" style="display:
inline;height:16px;cursor:pointer; width: 16px;"
onclick="ViewLookUpPayment('<%=btnShowCorpCode.ClientID %>');" />
function ViewLookUpPayment(obj)
{
document.getElementById(obj).style.display = "block";
}
I have this code inside a php file:
<script>
var myString = '{$data10}';
if (myString.charAt(0)) {
document.write('<a target="_blank"
href="https://www.somedomain.com/join?id={$data10}">
<img src="images/join-bim.png" width="300" height="74"
onMouseOut="this.src="images/join-bim.png""
onMouseOver="this.src="images/join-bimB.png"" /></a>');
} else {
document.write('<a target="_blank"
href="https://www.somedomain.com/join?id=111111">
<img src="images/join-bim.png" width="300" height="74"
onMouseOut="this.src="images/join-bim.png""
onMouseOver="this.src="images/join-bimB.png"" /></a>');
}
</script>
I have tried:
onMouseOut="this.src="images/join-bim.png""
but encountered problems due to the quotes not nesting.
Because of that, I then tried:
onMouseOut="this.src='images/join-bim.png'"
However, the single quotes are not being accepted.
you must escape inner quotes
onMouseOver="this.src=\"images/join-bimB.png\""
You can use escaped single quotes, as you can see below
<script>
var myString = '{$data10}';
if(myString.charAt(0)){
document.write('<a target="_blank" href="https://www.somedomain.com/join?id={$data10}"><img src="images/join-bim.png" width="300" height="74" onMouseOut="this.src=\'images/join-bim.png\'" onMouseOver="this.src=\'images/join-bimB.png\'" /></a>');
}
else {
document.write('<a target="_blank" href="https://www.somedomain.com/join?id=111111"><img src="images/join-bim.png" width="300" height="74" onMouseOut="this.src=\'images/join-bim.png\'" onMouseOver="this.src=\'images/join-bimB.png\'" /></a>');
}
</script>
The quotes are not escaped, meaning your quotes inside is closing the first quote, making PHP confused.
To escape it, use for your inner-single quotes:
onMouseOver=\'this.src="images/join-bimB.png"\'
I have an href that I would like to use with in an onclick method on the same anchor.
Here's an anchor:
<img alt="Best Practice 3" src="timg/BestPractice3.jpg" style="width: 236px; height: 157px" /> </td>
and here's the code the onclick is using:
function VideoTrackNLog(videoname, filename, file) {
frmVideoTrackNLog.videoname.value = videoname;
frmVideoTrackNLog.nextpagename.value = filename;
frmVideoTrackNLog.submit();
window.open(file,'Video','height=200,width=200,left=190,top=110,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
}
The idea is to move the URL in the href to the third parameter in the function. Is there any way this would be possible?
VideoTrackNLog('Best Practices 3','www.google.com', this.href);
Leave the href blank and move the URL to be a function parameter:
<a href=# onclick="VideoTrackNLog('Best Practices 3','www.google.com', 'http://www.google.com');">
<img alt="Best Practice 3" src="timg/BestPractice3.jpg" style="width: 236px; height: 157px" />
</a>
From the following string I need the dynamically changing "6.903" number to make calculation with.
Is there some regular expression or some jQuery trick to do that easily or elegantly?
<a href="javascript:void(0)" class="" id="buddyTrigger">
38.760 <img border="0" align="absmiddle" alt="Arany" src="/img/symbols/res2.gif">
5 <img border="0" align="absmiddle" alt="Pokolkristály" src="/img/symbols/res3.gif">
220 <img border="0" align="absmiddle" alt="Szilánk" src="/img/symbols/res_splinters.png">
91 / 125 <img border="0" align="absmiddle" alt="Akciópont" src="/img/symbols/ap.gif">
6.903 / 82.100 <img border="0" align="absmiddle" alt="Életerő" src="/img/symbols/herz.png">
<img border="0" align="absmiddle" alt="Szint" src="/img/symbols/level.gif"> 41
<img border="0" align="absmiddle" alt="Harci érték" src="/img/symbols/fightvalue.gif"> 878</a>
Here is my code as lenghty solution, can I simplify somehow?
<script type="text/javascript">
var dataIn=$('#infobar div.gold').html();
var dataPrep2Split=dataIn.replace(/<img(.*)>/g,';');
var dataSplit=dataPrep2Split.split(';');
var myData2Int=parseInt(dataSplit[18].replace('.',''));
if(myData2Int<=10000) {
$('#player').find('button.btn').remove();
var putBack=dataIn.replace(dataSplit[18],'<span class="newmessage">'+dataSplit[18]+'</span>');
$('#infobar div.gold').html(putBack);
}
</script>
Use DOM methods; replacing things using .html() often breaks page features. Also, that regex is liable to break with the smallest change.
You're trying to grab the Life value right? And that ends with the <img> with alt="Életero".
So that text node is (based on the Q code):
var lifeValTxtNd = $("#buddyTrigger img[alt='Életero']")[0].previousSibling;
And this gets 6903 from the contents like 6.903 / 82.100:
var lifeVal = $.trim (lifeValTxtNd.nodeValue)
.replace (/^\s*(\d*)\.?(\d+)\s*\/.+$/, "$1$2")
;
lifeVal = parseInt (lifeVal, 10);
Then to wrap that section in a span use:
$("#buddyTrigger img[alt='Életero']").before (
'<span class="newmessage">' + lifeValTxtNd.nodeValue + '</span>'
);
lifeValTxtNd.parentNode.removeChild (lifeValTxtNd);
Doing it this way:
Won't break any event listeners on the page.
Is less susceptible to changes in the page layout/content.
Is easier to understand and maintain.
Will run faster, if performance is a factor.
I am trying to put some space to the left of the radio-play.gif button. What can add to this code to achieve that?
Thanks!
// Last (only one on Mac) row has the VCR buttons.
//
s += '<td>';
s += '<img border="0" src="/images/dot.gif" width="81" height="' + gPreBtnVertSpace + '"><br>';
s += '<img alt="PLAY" src="' + imageDir + 'radio-play.gif" width="72" border="0" padding="5" height="61" style="cursor:pointer; cursor:hand" onclick="HandleAction(\'playnow\')">';
if (player != 'MP3')
s += '<img alt="STOP" src="' + imageDir + 'radio-stop.gif" width="72" border="0" height="61" style="cursor:pointer; cursor:hand" onclick="HandleAction(\'stop\')">';
s += '</td></tr>';
document.write(s);
// removing mute button
var myEl = document.getElementsByName("mute");
var elP = myEl[0].parentNode.parentNode;
elP.removeChild(myEl[0].parentNode);
Either set a margin to the img tag (it needs to be display:inline-block; for this) or add
a (No breaking space).
Probably the margin would be my preferred way, e.g.
img{
display:inline-block;
margin-left:5px;
}
or
s += ' <img alt="PLAY" ...
Btw.: The correct way would be, to create the <td> and <img> elements via document.createElement and then attach them to the dom. (Or use jquery, it's a bit simpler there)
You can literally put a space character infront of it. I would do it using CSS. Give the image a class class="whatever" and then in CSS:
.whatever {
margin-left: 10px;
}
Since you're doing it inline already, you could just add the margin in the inline css.
s += ' <img alt="PLAY" src="' + imageDir + 'radio-play.gif" width="72" border="0" padding="5" height="61" style="cursor:pointer; cursor:hand" onclick="HandleAction(\'playnow\')">';
OR, more correctly,
s += ' <img alt="PLAY" src="' + imageDir + 'radio-play.gif" width="72" border="0" padding="5" height="61" style="cursor:pointer; margin-left:5px;" onclick="HandleAction(\'playnow\')">';
If "space" means visually rendered space to the left of the rendered button element, this would typically be done with CSS. A common implementation would be that the image tag itself, or a container of the image tag, has a CSS class attribute that assigns space appropriately. For the CSS to do this, look into things like padding, margins, absolute vs relative positioning, the left or right attributes, etc.