Will this javascript do anything or is it incomplete? - javascript

I've been asked to include the following javascript in a project to make a widget appear:
<div class="nd-service nd-tyre-booking">
<script type="text/javascript">
var sourceDir = 'js/widgets/';
var widget = [
'http://<company-domain-is-here>.co.uk/',
sourceDir,
['tyre-booking', '4a68ff678d3d8fae7615519c977e5a5490cca00b']
];
var s = 'script', o = document.createElement(s);
o.type = 'text/javascript';
o.src = widget[0] + widget[1] + 'lib/require.js?';
o.setAttribute('data-main', widget[0] + widget[1]
document.getElementsByTagName(s)[0].parentNode.appendChild(o);</script>
Nothing happens though. I don't really know any javascript but it looks incomplete to me. It's referring to directories/files that don't exist such as js/widgets/ and lib/require.js
I've been back and forth with them asking for any kind of documentation and confirming that this is all that's required to make the widget appear, but they insist that it just needs to be pasted somewhere in the body and then it'll appear.
My javascript console outputs Uncaught SyntaxError: Unexpected identifier on the final line of javascript.

The line .setAttribute('data-main', widget[0] + widget[1] misses ")" and a ";".
It should be o.setAttribute('data-main', widget[0] + widget[1]);
And about the "does the script do anything" part, yes, it does. It defines a new script, fills it with something but... I fail to see the part where it is appended to the body.

Related

Loading a JavaScript file dynamically

I was asked to add this code to my pitch pages by the vendor I sell through:
<script>
(function() {
var p = '/?vendor=2knowmysel&time=' + new Date().getTime();
var cb = document.createElement('script'); cb.type = 'text/javascript';
cb.src = '//header.clickbank.net' + p;
document.getElementsByTagName('head')[0].appendChild(cb);
})();
</script>
The code should let the page load within a header that has a red logo by clickbank. When I added the code in the head section nothing happened.
Next I tried to isolate the problem by posting on a blank html page (away from drupal) which is http://www.2knowmyself.com/testpage.htm.
But the frame doesn't show up.
What's wrong in here? Given clickbank claim the code is perfect.
Here's what your code does:
<script>
// the following line creates an anonymous immediately-invoked function
(function() {
// this will return a string named 'p', which contains the vendors ID and current time
var p = '/?vendor=2knowmysel&time=' + new Date().getTime();
// this creates a new 'script' tag for HTML, name it 'cb', and tells the code it's for JavaScript
var cb = document.createElement('script'); cb.type = 'text/javascript';
// this will take a url with the address and the query string, which you named 'p' earlier, and set it as the source for 'cb'
cb.src = '//header.clickbank.net' + p;
// now you'll insert 'cb' to the HTML, so it'll load the JavaScript file into it
document.getElementsByTagName('head')[0].appendChild(cb);
// the function won't run automatically upon declaration, so you use parenthesis to tell it to run
})();
</script>
Summing it up, it bassically sends the vendor's ID and current time to the given server, and expects a JavaScript file in return from it; it'll then load this file into your HTML document.
Currently, it seems not to be working because this server is getting the information from your page but not sending the JavaScript file back to it. When they adjust it to answer with the right file, you'll see it run accordingly.
EDIT: (to answer your Final Question)
Up to this point, I can see that their server isn't sending the expected JS file back at your page, so it doesn't work. If you want to check this by yourself, please use a JS debugger or a network monitor in your browser (most of the modern webbrowsers come with these built-in, try pressing F12 then reloading the page).
If you want to check whether iframes work on your server, you may contact its administrator or try to embed an iframe in the page yourself. Paste the following code into the document. If you see SO homepage, it works. Otherwise, it'll show nothing. If you see Your browser does not support iframes., then you might have to update your web browser and check it again.
<iframe src="http://stackoverflow.com" width="300" height="300">
<p>Your browser does not support iframes.</p>
</iframe>

Javascript JSON return error with unescaped character

Hello and thank you for the time you are taking to look at this.
The problem I am having is with a JSON return from a third party service so I have no control over how the JSON is formatted. I am using the following (stripped down to the important pieces)
function JSONgrabber(Zip,iplocation)
{
var requestBase ="http://www.someCompany.com/api/request";
var requestData ="iplocate=true&output=json&callback=useJSON";
var request=requestBase+"&&zip="+Zip+"&"+requestData;
if(iplocation ==false || iplocation=="false")
{
request=request.replace("iplocate=true","iplocate=false");
}
if (document.getElementById("JSONDataMaster"))
{
var element = document.getElementById("JSONDataMaster");
element.parentNode.removeChild(element);
}
try{
var head = document.getElementsByTagName("head").item(0);
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
script.setAttribute("id", "JSONDataMaster");
head.appendChild(script);
}
catch(err){console.log("Problem loading JSON Data");}
}
function useJSON(JSONDATA){}
This works 99% of the time just as expected but on occasion the return has something like
Joe's Diner and the ' causes a Uncaught SyntaxError: Unexpected identifier, this happens during the return so I have not been able to work with it via replace etc to fix it as it never gets to the callback function.
any insight is much appreciated. I am not using any outside libraries (jquery etc) so please any help needs to be pure JS (perhaps php)
you need to check whether the string you want to get contains special chars and if so you need to escape them
this is a good page that can help to understand:
art of the web
this issue is similar:
stacko
if you check every string for a single quote and replace it with the escaped single quote:
mystring.replace("'","\\'");

Why does dojo.xhrGet needs different kinds of url to work on different computers (pc/mac)?

i'm writing an greasemonkey script for somebody else. he is a moderator and i am not. and the script will help him do some moderating things.
now the script works for me. as far as it can work for me.(as i am not a mod)
but even those things that work for me are not working for him..
i checked his version of greasemonkey plugin and firefox and he is up to date.
only thing that's really different is that i'm on a mac and he is pc, but i wouldn't think that would be any problem.
this is one of the functions that is not working for him. he does gets the first and third GM_log message. but not the second one ("got some(1) ..").
kmmh.trackNames = function(){
GM_log("starting to get names from the first "+kmmh.topAmount+" page(s) from leaderboard.");
kmmh.leaderboardlist = [];
for (var p=1; p<=(kmmh.topAmount); p++){
var page = "http://www.somegamesite.com/leaderboard?page="+ p;
var boardHTML = "";
dojo.xhrGet({
url: page,
sync: true,
load: function(response){
boardHTML = response;
GM_log("got some (1) => "+boardHTML.length);
},
handleAs: "text"
});
GM_log("got some (2) => "+boardHTML.length);
//create dummy div and place leaderboard html in there
var dummy = dojo.create('div', { innerHTML: boardHTML });
//search through it
var searchN = dojo.query('.notcurrent', dummy).forEach(function(node,index){
if(index >= 10){
kmmh.leaderboardlist.push(node.textContent); // add names to array
}
});
}
GM_log("all names from "+ kmmh.topAmount +" page(s) of leaderboard ==> "+ kmmh.leaderboardlist);
does anyone have any idea what could be causing this ??
EDIT: i know i had to write according to what he would see on his mod screen. so i asked him to copy paste source of pages and so on. and besides that, this part of the script is not depending on being a mod or not.
i got everything else working for him. just this function still doesn't on neither of his pc's.
EDIT2 (changed question): OK. so after some more trial and error, i got it to work, but it's still weird.
when i removed the www-part of the url thats being use in the dojo.xhrGet() i got the finally the same error he got. so i had him add www to his and now it works.
the odd thing is he now uses a script with the url containing "www" and i'm using a script with an url without "www"...
so for me:
var page = "http://somegamesite.com/leaderboard?page="+ p;
and for him:
var page = "http://www.somegamesite.com/leaderboard?page="+ p;
Why don't you have him try logging into an account that is not a moderator account so that you eliminate one of your variables from your problem space.
It's possible that the DOM of the page is different for a moderator than for a regular user. If you're making assumptions about the page as a regular user that are not true as a moderator, that could cause problems.
I suspect that to fix it, you may need access to a moderator account so you can more easily replicate the behavior.
ooops. it seemed that the url of this gamesite is accessible as www.gamesite.com as well as gamesite.com (without the www.part). this caused the problem.
sorry to bother you'all.
i go hide in shame now...

Facebook like : Uncaught TypeError: Object #<an Object> has no method 'provide'

I have recently added the facebook like button, but the following code returns an error in chrome: Uncaught TypeError: Object # has no method 'provide'
<!-- Facebook -->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '121814204514513', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<!-- Facebook -->
The like button works but the error is anoying, does anyone know how to solve that?
Thanks
I recently got the same problem when I tried to inject http://connect.facebook.net/en_US/all.js into Google Reader ( for this exciting Kynetx coding contest : http://code.kynetx.com/2011/04/26/250-to-build-kynetx-facebook-send-within-24hrs-ends-apr-27th/ ) . all.js starts with "if (!window.FB) window.FB = { ..." and declares the 'provide' method . In google reader the FB object was already present (dont know why or how it was created) so the code inside the if never got executed. The trick I used was to set FB to null before including "http://http://connect.facebook.net/en_US/all.js" . Google Reader didn't complain. It might be a solution in your situation too.
Update: you may need to set FB to null this way:
var head = $("head").get(0); // using jquery
var script2 = document.createElement("script");
script2.innerHTML = "window.FB = null;FB=null;";
head.appendChild(script2);
Although, the code provided by Loic Devaux will do the trick it is not needed in your case. He is solving an issue with markup shown on a third party website...
I had the same problem and initially added this code to solve it. Then I thought that there must be something wrong and I wasn't doing the things right. I added facebook comment box to a page that already had a facebook share button. So I just moved the share script after the initialization script and the error disappeared.
My advice is to check whether you have more than one FB widget on your page(s). If so read the documentation and make sure you've added the correct scripts on the correct place.
Loic's solution does not work in IE. Instead you have to do this:
var head = $("head").get(0); // using jquery
var script2 = document.createElement("script");
if (!$.browser.msie ) {
script2.innerHTML = "window.FB = null;FB=null;";
}else{
script2.text = "window.FB = null;FB=null;";
}
head.appendChild(script2);

Get info (document.getElementById) from an iFrame

I am getting the value "msg" from the page http://www.kimi007.freeiz.com/frame.html with the following code (address must be in the address bar and then you hit 'enter', thus getting the alert message "success!"):
javascript:
(function()
{
var%20s=document.createElement("script");
s.setAttribute("type","text/javascript");
s.setAttribute("src","http://www.kimi007.freeiz.com/java.js");
document.getElementsByTagName("head")[0].appendChild(s);
}
)()
The above has these two lines in the .js file:
var capForm = document.getElementById('form').elements[0].value;
alert('' + capForm + '');
I have no problems up to this stage. However I want to put "frame.html" in an iFrame like this:
http://www.kimi007.freeiz.com/test.html
and have my Java code work so I can get the same "success!" message from within the iFrame. Any ideas on how to do this?
Thanks.
try the following code, might do the trick:
top.frames[name_of_the_frame].some_function()

Categories