google.load - and message "google is not defined" - javascript

What do I need to include to do a google.load() statement?
I'm getting the error:
google is not defined
Based on this page, I thought I should add this:
<script type="text/javascript"
src="http://www.google.com/jsapi?key=ABCDEFG">
</script>
But when I did, I got this error:
"window.LoadFirebugConsole" is not a function.

I had the same problem and solved it like this:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type='text/javascript'>
function LoadGoogle()
{
if(typeof google != 'undefined' && google && google.load)
{
// Now you can use google.load() here...
}
else
{
// Retry later...
setTimeout(LoadGoogle, 30);
}
}
LoadGoogle();
</script>
The idea is to retry until google is defined.
The other solutions didn't help me, probably because this piece of code is loaded via Ajax from another page.

Did you include the google jsapi script before adding the load and callback methods? They should be in seperate script blocks.
<script src="http://www.google.com/jsapi?key=ABCDE"></script>
<script type="text/javascript">
google.load("jquery", "1");
// Define our onLoad callback
function OnLoad(){
alert("Loaded!");
}
google.setOnLoadCallback(OnLoad);
</script>
There are additional examples in the Google's 'AJAX Api's Playground'.

you should include this script -- http://www.google.com/jsapi

I had the problem, but I was using:
<script type="text/javascript" src="http://www.google.com/jsapi" />
It was solved by chanching the line to:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

Related

Caman was used before it was defined

In my HTML I have the following scripts in the header:
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="js/caman.full.js"></script>
and in my Javascript file (scripts.js) I tried to do the following code snippet:
Caman("#myImage", function () {
this.brightness(5);
}
As the title says, I get the error Caman was used before it was defined. I'm not sure if there's something obvious that I'm missing or doing incorrectly, but any advice would be appreciated.
The Caman script needs to be included first like this:
<script type="text/javascript" src="js/caman.full.js"></script>
<script type="text/javascript" src="scripts.js"></script>
Then in your script you are missing the last );:
Caman("#myImage", function () {
this.brightness(5);
});
If that doesn't work there may be something else in your code that is causing problems.

Run File From Address Using hostname

there is a script I'm trying to use for Blogger. It works when you enter your domain URL into the src, but I'm trying to find a way to do it using hostname to insert the domain.
Original script:
<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=INSERT-YOUR-URL-HERE&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json" />
I tried:
<script type="text/javascript">
var excuteTopCommentators = "http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json"
return excuteTopCommentators
</script>
I also tried document.write:
<script type="text/javascript">
document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json"></script>');
</script>
Neither of the attempts seem to work. Any ideas on how to do this without manually inserting the domain URL into the script src?
Perhaps the problem is that you get the script, but it is never executed.
Maybe try jQuery's getScript http://api.jquery.com/jquery.getscript/
function getIt() {
return $.getScript('http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json');
}
Your second option looks fine:
<script type="text/javascript">
document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json"></script>');
</script>
The script is downloaded but do nothing. this is because it is a JSONP, not an script
So, you need a callback function to get it working: (note that is data, not code)
<script type="text/javascript">
// JSONP Callback
function getYpipePP(data) {
alert(JSON.stringify(data));
}
document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json"></script>');
</script>
http://plnkr.co/edit/HhYZtH09EcdPZbwS9rfq?p=preview
Look the query, say _callback=getYpipePP and _render=json
Related: What is JSONP all about?
Try this (the problem you're having is the end "script" tag):
<script type="text/javascript">
document.write('<script language="javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'
+ window.location.hostname
+ '&ShowHowMany=5&_id=390e906036f48772b2ed4b5d837af4cd&_callback=getYpipePP&_render=json" type="text/javascript"><\/script>')
</script>

Wait for Javascript load from CDN

I have this block of javascript in a Rails app, with turbo-link enabled:
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.0/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/be7019ee387/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<script type="text/javascript" charset="utf-8">
var ready = function() {
setTimeout(function() {
$('#table-products').dataTable();
}, 100);
};
$(document).on('ready, page:change', ready);
</script>
Usually, I get the error that the .dataTable() function is undefined, and a refresh can solve the problem. However, refresh is not always pleasant, so I added the setTimeout for 100ms to wait for the js from CDN to load.
So far I don't see any issue in my dev environment, but I am afraid that if there is a slow client, the js won't finish loading within 100ms.
I wonder if there would be a better way to solve this, like checking if the two js files have been loaded, similar to $(document).ready for DOM.
Thanks!
There's actually a gem for this. It's really easy to use and comes with bootstrap styling options. This is more elegant than what you have because it doesn't rely on CDNs and instead leverages the Rails asset pipeline.
Try
<head>
<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.0/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/be7019ee387/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<script type="text/javascript" charset="utf-8">
$.holdReady(true);
var s = setInterval(function() {
if ($.fn.hasOwnProperty("DataTable")
&& typeof $.fn.DataTable === "function") {
$.holdReady(false);
clearInterval(s);
};
}, 1);
$(document).ready(function() {
$("#table-products").dataTable();
});
</script>
</head>
jsfiddle http://jsfiddle.net/guest271314/RZVRL/
See https://api.jquery.com/jQuery.holdReady/
The most elegant way I can find is to move all the cdn links to the layout page in the , so that all the libraries will be loaded and cached for the entire user experience.

Add Try Catch to Javascripts with SRC

I have these CODE#1 scripts and the first script has a source that is running on a web service. I need to have a try catch so when the web service goes to a site down, I can make an else condition like in CODE#2. How can I achieve this. Thanks
CODE #1
<script type="text/javascript" language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity">
</script>
<script type="text/javascript" language="Javascript">
document.write("<p>Welcome to visitors from "+sGeobytesCity+", " + sGeobytesCountry);
</script>
CODE #2
TRY
{
<script type="text/javascript" language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity">
</script>
<script type="text/javascript" language="Javascript">
document.write("<p>Welcome to visitors from "+sGeobytesCity+", " + sGeobytesCountry);
</script>
}
catch
{
window.location = "geobytes.aspx";
}
You can’t use try/catch this way. (Especially since you put it outside of the script elements, so it would be part of the HTML code, and HTML is not a programming language and knows no such thing as a try/catch construct.)
But if the first script is supposed to create the variables sGeobytesCity and sGeobytesCountry that your second script is trying to output, you could check whether they exist or not first in your second script:
if(typeof sGeobytesCity !== "undefined") {
document.write("<p>Welcome to visitors from "+sGeobytesCity+", " +
sGeobytesCountry);
}
else {
window.location.href = "geobytes.aspx";
}

Problem with RegisterClientScriptInclude and RegisterClientScriptBlock

I Have usercontrol and i registered the following javascript method in code behind :
changeSelectedMenu(controlID);
the previous method is declared in JScript.js file so i registered it also .
but the following exception has been thrown :
Microsoft JScript runtime error: Object expected
The result page is :
<script src="~/Scripts/JQuery.js" type="text/javascript"></script>
<script src="~/Scripts/JScript.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
changeSelectedMenu('ctl00_ctl00_cntMain_procedureContentHolder_procedureMenu_appointmentsLink');//]]>
</script>
Is There anyone can help me to workaround this issue???
These aren't valid paths to your JavaScript:
<script src="~/Scripts/JQuery.js" type="text/javascript"></script>
<script src="~/Scripts/JScript.js" type="text/javascript"></script>
They need to be relative to the page or to the site root, for example:
<script src="/Scripts/JQuery.js" type="text/javascript"></script>
<script src="/Scripts/JScript.js" type="text/javascript"></script>
While ~/ works for resolving a path on the server-side, the browser doesn't know how to handle this. There are some other work-arounds in this question for making it work and still being app-relative.

Categories