javascript fails to execute mapquest api - javascript

I would like for this code to alert the users with their perspective addresses, but for some reason it's not executing. Can someone point me on the right direction?
Thanks!
<html>
<head>
<title>geoload</title>
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE=40.0755&lng=-76.329999&output=json&callback=renderGeocode"></script>
<script type="text/javascript">
function renderGeocode(response){
var location = response.results[0].locations[0];
alert(location.adminArea5 + ", " + location.adminArea4);
}
</script>
</head>
<body onload=(load"renderGeocode")></body>
</html>

You've missed a + sign:
alert(location.adminArea5 + ", " + location.adminArea4);
^

The body onload is not required, as the call to the mapquestapi includes a callback parameter. The callback parameter is renderGeocode which will call the javascript function of the same name. See code below:
<html>
<head>
<title>geoload</title>
<script type="text/javascript" >
function renderGeocode(response) {
console.log(response)
}
</script>
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE&lat=40.0755&lng=-76.329999&callback=renderGeocode" ></script>
</head>
<body></body>
</html>

Related

LinkedIn example code gives SyntaxError

Trying to follow a linkedin example but their code does not seem to work
When I copy the example code into a HTML page it gets rendered as this
Profile App Example
'); IN.parse(document.getElementById("profile")) }) }
I get this error in firebug
SyntaxError: unterminated string literal
[Break On This Error]
e="IN/FullMemberProfile" data-id="' + result.values[0].id + '">
but I am not seeing it(in the example code it looks like all the quotes match).
Also get this error
TypeError: $.cookie is not a function
[Break On This Error]
...kie('quck-note-current', JSON.stringify({ "Id": id, "Note": note }), { expires: ...
Edit
Here is the full example code they have.
<html>
<head>
<title>Profile App Example</title>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: o1yf9WMdgd8dp_OGkmtXESCOJFostN8N1jI1AFKY2i0kJ1QFNMOs3a6R5qUoBIqF
authorize: true
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js"></script>
<script type="text/javascript">
function loadData() {
IN.API.Profile("me")
.result(function(result) {
$("#profile").html('<script type="IN/FullMemberProfile" data-id="' + result.values[0].id + '"></script>');
IN.parse(document.getElementById("profile"))
})
}
</script>
</head>
<body class="yui3-skin-sam yui-skin-sam">
<div id="profile"></div>
<script type="IN/Login" data-onAuth="loadData"></script>
</body>
</html>
Edit 2
I tried using their api key and I used my api key(what is for some reason alot shorter than their example on but that is what it is showing in the app area under api key)
jQuery removes the script tag when using $(string).
This line:
$("#profile").html('<script type="IN/FullMemberProfile" data-id="' + result.values[0].id + '"></script>');
needs to have </script> becoming <\/script>, which is why the error was found just before:
'); IN.parse(document.getElementById("profile")) }) }
Similar discussion: How to get JavaScript script tags inside jQuery .html() function?

String.format not a function

I'm using HTML publisher in hope to have a html page with some javascript codes running on hudson. The HTML code is like this:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../stringformat.js"></script>
<script type="text/javascript">
......
sql=String.format("/.csv? select from table where exchange=`ABC......")
</script>
</head>
However, after a successful build, my html page doesn't show what it suppose to, and as I check the error console, it says
Error: TypeError: String.format is not a function
I have put my stringformat.js into the top folder, as the HTML publisher doesn't seem to allow the file to contain anything other than HTML files.
Can anyone tell me why the String.format is not loaded properly? Thanks!
PS: stringformat.js is the file i got from
http://www.masterdata.se/r/string_format_for_javascript/
The code should be working properly as this piece of code works outside the hudson
try code:
<html>
<head>
<title>String format javascript</title>
<script type="text/javascript">
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
var _myString = String.format("hi {0}, i'am {1}","everybody", "stackoverflower");
function show(){
alert(_myString);
}
</script>
</head>
<body>
<input type="button" name="btnTest" id="btnTest" value="Test string format" onclick="show();" />
</body>
</html>

Splitting up a Javascript snippet

The recommendation of the open source web analysis software Piwik is to put the following code at the end of the pages you want to track, directly before the closing </body> tag:
<html>
<head>
[...]
</head>
<body>
[...]
<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://piwik.example.com/" : "http://piwik.example.com/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 4);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="http://piwik.example.com/piwik.php?idsite=4" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
</body>
</html>
Under the following assumptions:
https is never used
we don't care that the page loads slower because the script is loaded before the DOM
is it okay to convert the above to the following:
HTML file:
<html>
<head>
[...]
<script src="http://piwik.example.com/piwik.js" type="text/javascript"></script>
</head>
<body>
[...]
<noscript><p><img src="http://piwik.example.com/piwik.php?idsite=4" style="border:0" alt="" /></p></noscript>
</body>
</html>
Custom Javascript file with jQuery:
$(document).ready(function() {
try {
var piwikTracker = Piwik.getTracker("http://piwik.example.com/piwik.php", 4);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
}
catch(err) {
}
}
Are there any differences?
You are deferring the tracking until the page is fully loaded. Inline Javascript is executed when the browser finds it, so you'll have different number of visits depending on where you call piwikTracker.trackPageView();. The latter you call it, the lesser number of visits/actions will be counted.
Now, what do you consider a visit/action? If a user click on a link on your page, before the page fully loads, do you consider it a visit?

What's wrong with this short Javascript code?

I'm trying to make the short Javascript code throw an alert and show 93 instead of 0-93 but it's not working ?
<html>
<head>
<script type="text/javascript">
function numberFromInput(value) {
return alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
</script>
</head>
<body>
numberFromInput(0-93);
</body>
</html>
You have to call the function (you're just displaying its call code as content). And you have to pass the value as a string (you need quotes around 0-93):
<script type="text/javascript">
function numberFromInput(value) {
return alert(Number(value.match(/[-]?(\d*)$/).pop()));
}
numberFromInput("0-93");
</script>

help: when calling a javascript functions, it's interrupted by jquery's onready method!

wow! i just noticed something, every time i try to call a function, no matter what function, in javascript, somehow jquery and ms ajax framework javascript captures it and checks if the document is ready (document.onready or other) and never returns the control back to the function im calling, or whatever it does, my function never ends up getting called! why on earth is it doing this? i've never asked for it to!!!
all i have is references to these libraries, script/link references as you do on the top of your master page.
thsi is ridiculous, anyone have any ideas?
here is the code breaks when calling UpdateGlobalVariables
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.superload.js" type="text/javascript"></script>
<script src="~/Views/Shared/JScript.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var RatePicPanelRunCount = 0;
function ChangeMainPic(newSrc) {
$get("imgPic").src = newSrc;
alert($get("imgPic").src + '\n' + newSrc);
RatePicPanelRunCount++;
}
function UpdateGlobalVariables() {
// Update variables...
ShownPicID = <%=Model.CurShownPicID%>;
ShownUserID = <%= Model.CurShownUserID %>;
CurrentUserID = <%= UserID %>;
alert('CurUserID is ' + CurrentUserID);
alert('From cookie its ' + getCookie('UserID'));
}
debugger;
if (RatePicPanelRunCount == 0) {
ChangeMainPic('<%= Model.CurPicURL%>');
UpdateGlobalVariables;
};
</script>
At the very bottom of your script, shouldn't you be calling UpdateGlobalVariables();, not UpdateGlobalVariables;?

Categories

Resources