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.
Related
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.
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'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
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>
Here is a Google Analytics' code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20366831-2']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
How my client side calls Google anonymous function?
It's called because the anonymous function ends with ()
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); // <--- The () calls the anonymous code
As you'll see, this code basically injects a script tag into the DOM, which gets run by the browser.
That snippet already call itself.
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
what it actually does is including the ga.js on your page, which is similar to this:
<script src="//google-analytics.com/ga.js" />
The rest is up to you to add event to the _gaq (google analytic queue). Then the event will automatically be processed.