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>
Related
I'm looking for a way to select and load a javascript from the html file arguments. The html file is called as follows:
OSM_Map.html?year=2017 or OSM_Map.html?year=2018
In the OSM_Map.html file there is the following code in the header:
<head>
.....
<script language="JavaScript" type="text/javascript" src="LatLonDB_2017.js"></script>
<script language="JavaScript" type="text/javascript" src="LatLonDB_2018.js"></script>
....
</head>
There is no problem to get the year argument from the argument list, but how can I load depending on the year argument just one of these .js files?
As somebody said, "yes, you can":
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/loadjs/3.5.5/loadjs.min.js"></script>
<script>
var myparameter = new URL(location.href).searchParams.get("year");
loadjs( "LatLonDB_" + myparameter +".js" );
</script>
</head>
<body>
<h1>Titulo</h1>
</body>
</html>
Been the URL something like: http://test.html?year=2018
BUT! not sure if this will work in every browser.... the "searchParams" is not universally compatible.
Thanks to #spencer.sm in this question How to get the value from the GET parameters?
and of course, loadJS function.
The LatLonDB_xxxx.js script still doesn't load. I'm not sure why not. Cannot you load a .js file from another directory than where the .html file is? Otherwise the load may be too late. Scripts following the LatLonDB_xxxx.js script use this DB.
The original code is like this:
<html>
<head>
...
<script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_20xx.js"></script>
<script language="JavaScript" type="text/javascript" src="../DataBases/LatLonDB_utils.js"></script>
<script language="JavaScript" type="text/javascript" ...more scripts using the LatLonDB_20xx.js></script>
...
</head>
...
</html>
The intention is to replace the 20xx by the correct year: 2000, 2001, etc. There is no problem to get the correct year from the query parameters.
So lets say you're implementing a website that uses jQuery HEAVILY. You could put some code like
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
and import it from some repository. If you're developing it without internet you could download the source and store it somewhere locally, then access it with some script like
<script src="js/jquery-1.10.2.min.js"></script>
But is there a simple way to have both? Such as if you can reach the repository use that, but if you can't use the local copy.
Check for a variable in the first script. If it is not found, use document.write to create the second script tag. Here is an example for jQuery I found here:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="js/jquery-1.7.1.min.js"><\/script>')</script>
The fail-safe way of referencing scripts on a CDN is to link to the local copy only if the CDN has failed for any reason.
The way to do this is simply to check if anything within the script has executed. For jQuery this is simply checking whether jQuery exists:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
if (!window.jQuery) document.write('<script type="text/javascript" src="/path/to/jquery-ver.sion.min.js"><\/script>');
</script>
Personally I have never had a script fail due to a CDN being offline, however I have had periods of internet outage. With scripts set up with a proper fallback, I've been able to continue local development as the pages still work without needing to connect to a CDN.
You can add resources dynamically if required one is not available. for eg:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
if( typeof $ != "function")
{
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'js/jquery-1.10.2.min.js';
head.appendChild(script);
}
</script>
</head>
<body>
</body>
</html>
Though JQuery hosted on Google CDN should be safe enough, the codes below can be used as a fallback with requireJS.
requirejs.config({
enforceDefine: true,
paths: {
jquery: [
'//ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js',
'js/jquery-ui.min.js'
]
}
});
require(['jquery'], function ($) {
});
I am trying to load php code into div tag to show the results but the below code doesn't work for.
Can anyone please help me...to resolve this
my code is below:
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$("document").ready(function()
{
$("#myDiv").load("refreshing.php");
}
</script>
</head>
<body>
<div id='myDiv'> </div>
</body>
</html>
Change script tag to this
<script type="text/javascript">
$(document).ready(function()
{
$("#myDiv").load("refreshing.php");
});
</script>
i.e. you're missing ); at the end
Also, document should not have quotes on it
You cannot load php code .load() because as the web server gets the GET request with .php, the request is passed to PHP interpreter which will execute the PHP script and return the result of the php script instead of code. So what you can do is in your PHP script, do a echo of all the code that you want to display. Or, you can define custom rule in your Apache configuration.
You've forgotten the closing bracket:
$("document").ready(function(){
$("#myDiv").load("refreshing.php");
});
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";
}
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>