I have been having trouble getting the correct string formatted for my document.write()function. Any help is greatly appreciated. Jsfiddle link below of what i have so far. Below is what I am trying to achieve.
jfiddle
<img style="height:100px;" src="https://web.fulcrumapp.com/api/v2/photos/+ myarray[i]+.jpg?token=3c04f586f4991a" />
Your Fiddle code is currently putting the link text inside the link tag itself. You should close the > before putting the link text ;)
Additionally, don't use document.write...
Thank you, Niet for the advice I am looking into how to do this another way, but for now I was able to figure out my syntax.
document.write("<li><img src=\"" + url + "" + myarray[i] + "\"></li>");
Related
`jQuery('#resultWorkingDays').html(numberWithCommas(resultWorkingDays) + ' days')`
Using above prints 'days' on the end of a string. I want to change 'days' to an image source. I've searched & tried a lot of different things but none seem to work.
Complete JS novice, help much appreciated
Just include the HTML.
jQuery('#resultWorkingDays').html(numberWithCommas(resultWorkingDays) + ' <img src="yourImage.jpg">');
jQuery("#resultWorkingDays").html(numberWithCommas(resultWorkingDays) + "<img src='\images\...\name.png />");
can some one help me to fix this link in my javascript code
the code is
$('.frame_src').attr('src', 'https://www.youtube.com/embed/' + $(this).data('str'));
full link is https://www.youtube.com/embed/p9zdCra9gCE?&vq=hd720&rel=0;autoplay=1
i want to make it something like this i know it's wrong but i can't fix it
$('.frame_src').attr('src', 'https://www.youtube.com/embed/' '?&vq=hd720&rel=0;autoplay=1' + $(this).data('str'));
You're almost there, you need to insert the $(this).data('str') value in the right place.
Change
$('.frame_src').attr('src', 'https://www.youtube.com/embed/' '?&vq=hd720&rel=0;autoplay=1' + $(this).data('str'));
to
$('.frame_src').attr('src', 'https://www.youtube.com/embed/' + $(this).data('str') + '?&vq=hd720&rel=0;autoplay=1');
I'm using phonegap to share an article via WhatsApp.
The code for the button is as follows:
shareArticle += '<li class="rrssb-whatsapp"><a href="javascript: void(1)" onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress.com/showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});" class="popup" data-action="share/whatsapp/share">';
shareArticle += '<span class="rrssb-icon"><!-- Icon in SVG --></span>';
shareArticle += '</a></li>';
The part that I'm asking about is this:
onclick="window.plugins.socialsharing.shareViaWhatsApp(\''+$('.article_title').html().replace(/'/g, "'")+'\', null, \'http://www.myaddress.com/showArticle-'+articleId+'\', function() {console.log(\'share ok\')}, function(errormsg){alert(errormsg)});"
The button is not working when there is an apostrophe in the title.
The strangest thing is that if I replace ' with " it work perfectly (even thought the result is wrong).
Doe's anybody has any idead why ' fails?
Thank you all for your support.
The solution is to change the apostrophe to another sign that doesn't break the string.
So what I did is:
$('.opinion_content_title').html().replace(/'/g, "′")
Again, thank you all.
The named character reference ' (the apostrophe, U+0027) was introduced in XML 1.0 but does not appear in HTML. Authors should therefore use ' instead of ' to work as expected in HTML 4 user agents.
I have opened a new window, and now I have a button in this new window. Under this button there is a JavaScript function which should submit the form and insert some content into a <p>. This "to-add" content also has a html element (close button).
This inserting job is not working. Here is what I tried:
myWindow.document.write("<scrip" + "t>function closeme(){ document.getElementById('danke').innerHTML='thanks <input type=\'button\' onclick=\'window.close()\'/>'; } </sc" + "ript>");
Can someone please help me out?
As said in the comments there are better ways to insert scripts, but in the interests of answering the question, you have to escape the escape characters as well so that they will be present in the output.
myWindow.document.write("<scrip" + "t>function closeme(){ " +
"document.getElementById('danke').innerHTML='thanks <input type=\\\'button\\\' " +
"onclick=\\\'window.close()\\\'/>'; } </sc" + "ript>");
(line breaks added for readability)
I agree there are better ways of doing this, but regarding your specific question. There are escaping issues:
myWindow.document.write("<scrip" + "t>function closeme(){ document.getElementById('danke').innerHTML='thanks <input type=\"button\" onclick=\"window.close()\" />'; } </sc" + "ript>");
I'd honestly add text/javascript for consistency.
I'm working with a plugin that is only Javascript. I need to have it dynamically create a DIV element with an advertisement in it.
I can't figure out why this doesn't work:
$(this).append('<div class="overlay-background">Advertisement
<script type="text-javascript">
GA_googleFillSlot("blog_landing_right_rectangle_300x250");
</script>'
It results in the element created with "Hello World" but it does not execute the GA-googleFillSlot function.
appending HTML into the DOM does not cause the browser to evaluate any script tags in said appended HTML.
If you really wanted to, you could evaluate the javascript by using eval():
eval($(this).find("script").text());
I know this is an old question but I've had a similar problem today.
The solution was using createContextualFragment.
My code looks something like this:
var tagString = '<script async type="text/javascript" src="path_to_script"></script>';
var range = document.createRange();
range.selectNode(document.getElementsByTagName("BODY")[0]);
var documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);
This code works in my browser.
$('body').append('<script>alert("test");<' + '/' + 'script>');
so it might be that $(this) is what is actually causing your problem.
Can you replace it with 'body' and see if it works like that?
One workaround in your case could be to append a fake image with an onload event:
<img src="blank.gif" onload="GA_googleFillSlot('blog_landing_right_rectangle_300x250')" />
since your are in javascript, you can append and then execute the code:
$(this).append('<div class="overlay-background">Advertisement</div>');
GA_googleFillSlot("blog_landing_right_rectangle_300x250");
Please try:
s = '<' + 'script type="text-javascript">' +
'GA_googleFillSlot("blog_landing_right_rectangle_300x250");' +
'<' + '/' + 'script>';
$(this).append(s);
UPD: alas, this won't work, please use wless1's solution instead