Add Try Catch to Javascripts with SRC - javascript

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";
}

Related

Mixing of different sources in the same scripts

I'm trying to include a couple of conditions in my code excluding only android visitors. I put this code but the first script in else condition is not working.
<script type='text/javascript'>
if( /Android/.test(navigator.userAgent) ) {
// some code..
}else
{
<script type="text/javascript" src="link"></script>
<script type="text/javascript" src="link"></script>
}
</script>
You can't wrap raw html in an else
You could add them using document.write()
Something like:
if( /Android/.test(navigator.userAgent) ) {
// some code..
}else
{
document.write('<script type="text/javascript" src="link"></script>');
document.write('<script type="text/javascript" src="link"></script>');
}
You can't do that.
The easiest way to do what you want to achieve is to include all scripts and then execute its content conditionally.
If you can't control the source code of these scripts, then this is a duplicated question of How do I include a JavaScript file in another JavaScript file?

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>

Web Worker Javascript cannot find functions in other javascript files

I have an HTML file like this:
<html>
<head>
<title>World Aviation</title>
<script language="javascript" src="./jquery-min.js"> </script>
<script language="javascript" src="./jsDraw2D.js"> </script>
<script language="javascript" src="./script.js"> </script>
</head>
<body>
<input onClick="startAnimBackground();" type="button" value="Start"/>
<script language="javascript">
initAirports();
initRoutes();
var w;
function startAnimBackground()
{
if(typeof(Worker) !== "undefined")
{
if(typeof(w) == "undefined")
{
w = new Worker("start_script.js");
}
} else {
startAnimation();
}
}
</script>
</body>
</html>
the file "start_script.js" contains only 1 line which calls the startAnimation() function. This function is located in "script.js"
My problem is: my browser supports web-workers, so it creates a web-worker and loads the file "start_script.js", but it fails to call the function "startAnimation()" indicating that it cannot find the function. But this function is located in script.js which is included in the head section. So, the only conclusion I can draw is that web-worker JS files have a different scope and thus is not able to include the 3 javascript files specified in the head section. So what should I do to make this work?
Thanks.
Web workers don't share functions, variables, or scripts with the main script.
To load dependencies, call importScripts() in your worker.
To allow your web worker to interact with or affect the main script or the DOM, you have to use message passing and have the main script listen for, interpret, and act on the messages.
See: https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

Why will an object literal load when in declared in external source file but not when called in a file including an external javascript file file

The code in the external file is
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
In the php file I am using
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
But this will not work why?
if I call the function in the external fil it works
var testing = {
bugtest: function() {
alert('No Bugs Here');
}
}
testing.bugtest()
this will work but this not what I want it to do I want to be able to call the function in the main file? What would the cause of this problem be?
You can not use src attribute and the text node with script elements.
They must be exclusive, e.g. an element each.
So your HTML would look something like...
<script type="text/javascript" src="externalScript.js"></script>
<script type="text/javascript">
testing.bugtest();
</script>
This
<script type="text/javascript" src="externalScript.js">
testing.bugtest();
</script>
is wrong. You can either specify a src, or run inline code.

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

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>

Categories