javascript weirdness in IE8, document.write issue - javascript

Any idea why putting google plus code this way wont work in IE8 or any other version I guess? Works just fine in firefox though.
live demo: http://jsfiddle.net/9zqsZ/
<script>
var socialString = '<g:plusone size="tall"></g:plusone>';
document.write(socialString);
//google plus share button
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
I am just trying to put it in external file. The weird thing is it just does not work with document.write and works if is placed directly in html. How to implement it in that case?

You could do something like:
<script>
if (navigator.appVersion.indexOf("MSIE") != -1) {
var socialString = '<g:plusone size="tall"></g:plusone>';
var newEle = document.createElement(socialString);
document.body.appendChild(newEle);
}
else {
var socialString = '<g:plusone size="tall"></g:plusone>';
document.write(socialString);
}
//google plus share button
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

Related

How to get javascript result into php

I want to use the search engine of different newspapers sites to get all articles of newspapers which are selected by keyword and then delete articles that are too much similar.
I'm trying to use http://www.20minutes.fr/search?q=test search engine but as many other search engine the html result is generated by javascript:
<script>
(function() {
var cx = '011646568010423734157:bmweo2mlsou';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
and i cant access the url with curl.
How can i get these url ?
thank you for reading.

Hide Search Engine ID for Google Custom Search Element API 2.0

I am implementing Google Custom Search Element API 2.0 in an ASP.NET MVC project. In the documentation they require that the following javascript is included in the view, with the <gcse:search> element following.
<script>
(function() {
var cx = 'xxxxxxxx:yyyyyyyy';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search></gcse:search>
However, the search engine id is visible on the line:
var cx = 'xxxxxxxx:yyyyyyyy';
In a browser, selecting View Source (or similar) allows the user to view the script and the search engine id.
How do I ensure that no-one can see my id?
I have found a way around this. By moving the Google script to its own function that takes the Search Engine Id as a parameter:
function buildSearchElement(cx)
{
var gcse = document.createElement("script");
gcse.type = "text/javascript";
gcse.async = true;
gcse.src = (document.location.protocol === "https:" ? "https:" : "http:") +
"//www.google.com/cse/cse.js?cx=" + cx;
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(gcse, s);
};
I then call a method on my controller that returns the Search Engine Id upon page load. When this completes, the callback is the buildSearchElement function:
(function ()
{
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function()
{
if (httpRequest.readyState === 4 && httpRequest.status === 200)
{
buildSearchElement(httpRequest.responseText);
}
}
httpRequest.open("GET", "/GetSearchElementId");
httpRequest.send();
})();

google+ like button isn't working in django template

I used this code for my web page
<div class="g-plusone" size="small"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
but when I click on g+ button, nothing is happening, i mean no pop-up windows like when we do for facebook/twitter? am i missing something in this code?
No it is working perfectly,
Here is Demo
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
Debug the error using chrome developer tool. It is useful.

How to insert javascript code in iframe tag

I am doing like this but not working:-
I am trying to insert the pixel in iframe and then append it to the body.
jQuery('#subscribr').append('<iframe>
<script language="javascript" src="http://www.abc/static/st.v2.js"></script>
<script language="javascript">
var ef_event_type="transaction";
var ef_transaction_properties = "ev_ProductViews=1";
/*
* Do not modify below this line
*/
var ef_segment = "";
var ef_search_segment = "";
var ef_userid="xxxx";
var ef_pixel_host="pixel.abc.net";
var ef_fb_is_app = 0;
effp();
</script>
</iframe>');
enter code here
Scripts like that appear to break when </script> is included in the string. A workaround to that could be to add the elements through dom manipulation:
iframe = $('<iframe>');
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://www.abc/static/st.v2.js"; // Or:
script.text = "Your code here!"
iframe[0].appendChild(script);
iframe.appendTo('#subscribr'))
This is the solution which worked for me..
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://www.abc/static/st.v2.js";
// Use selector here
jQuery("#subscribr").append(s);
Working sample: http://fiddle.jshell.net/f2x7G/
var doc = null;
if(iframe.contentDocument) doc = iframe.contentDocument;
else if(iframe.contentWindow) doc = iframe.contentWindow.document;
else if(iframe.document) doc = iframe.document;
if(doc == null) throw "Document not initialized";
doc.open();
var script = doc.createElement("script");
script.type = "text/javascript";
// script.src = 'http://www.abc/static/st.v2.js';
script.text = "alert('voila!');"
doc.appendChild(script);
doc.close();

Google Custom Search - Search on querystring

Google's updated their custom search code last time I looked. Here's our working code:
<script>
(function () {
var cx = '011561302208175438083:iegdgk3oox8';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
...
<gcse:search></gcse:search>
What we want to know, is how do we make this search box automatically search for a value when the page loads?
<gcse:search queryParameterName='q' autoSearchOnLoad='true' noResultsString='no results'></gcse:search>
this will automatically search for the q=MySearchString in the URL

Categories