Reconcile load and reload Script.js - javascript

I am trying to reconcile this reload JS script found at Stack Overflow with a script used by my web app to read the load the script.js. Essentially the first cloudfare.com/ajax script says load the script.js and the second function says reload the script. Where source_code is the actual URL of the script.
The problem is it works 50% of the time. I know I am missing a something. It is so close.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
crossorigin="anonymous" async>
< function reload_js(src) {
$('script[src="' + src + '"]').remove();
$('<script>').attr('src', src).appendTo('head');
}
reload_js("source_code.js"); />
</script>

It work everytime but u did mistake by forgot a "<" in your code
function reload_js(src) {
$('script[src="' + src + '"]').remove();
$('<script>').attr('src', src).appendTo('head');
console.log($('head script')[0]);
console.log($('head script')[1]);
}
reload_js("source_code.js");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related

calling functions in html from external javascript file

This is a simple practical but I'm not sure what I'm doing wrong.
I have the following code in my external javascript file:
function myname(){
document.write("Monique");
}
function welcomeW(name){
document.write("Welcome " + name + "!");
}
function welcomeR(name){
return "Welcome " name "!";
}
I added this <script> tag to link to my html file:
<script src="script.js" type="text/javascript"></script>
I tried calling the functions in html using:
<script> myname(); </script>
<script> welcomeW(Monique); </script>
<script> welcomeR(Monique); </script>
when I wrote the function in html it worked, but in the external file nothing happens.
Monique , there is only one issue in your code, please use single quote (i.e 'Monique') or double quote ("Monique") when you are passing argument, i thing you are getting "Uncaught ReferenceError: Monique is not defined" error message.
<script> myname(); </script>
<script> welcomeW("Monique"); </script>
<script> welcomeR("Monique"); </script>
Or use this.
<script> myname(); </script>
<script> welcomeW('Monique'); </script>
<script> welcomeR('Monique'); </script>
If a <script> has a src then the text content of the element will be not be executed as JS (although it will appear in the DOM).
You need to use multiple script elements.
a <script> to load the external script
a <script> to hold your inline code (with the call to the function in
the external script)
You might have put the <script src="<your js file>" type="text/javascript"></script> at the wrong place in your html file. Try putting it in the <head></head> or before your body closing tag </body>.
Another common mistake is in <script src="<your js file>" type="text/javascript"></script> try entering the location of like for example: <script src="./index.js" type="text/javascript"></script> if your index.js file is in the same folder as your index.html.
Hope it helps!
From whay you described, you are calling your functions and linking the js file in Html correctly. There's just a couple things that are causing errors.
The welcomeR function has a littly syntax error. You are likely running into Uncaught SyntaxError: Unexpected identifier 'name' and *uncaught ReferenceError: myname is not defined". You needs to have "+" between the strings and name for the return statement of welcomeR to resolve this error. Use console.log() statements to check that your js file is running as you intended.
When you are calling the functions in your html file with , you should have an uncaught syntax error where Monique is not defined. You just need to change the welcomeW and welcomeR function from
<script> myname(); </script>
<script> welcomeW(Monique); </script>
<script> welcomeR(Monique); </script>
to
<script> myname(); </script>
<script> welcomeW("Monique"); </script>
<script> welcomeR("Monique"); </script>
so you are passing strings in.
With this, you should be able to get "MoniqueWelcome Monique!" for your output.
The last function welcomeR does not output anything into the output file because it is returning a str, and not changing the content of your external file. If you wish to have it also output into you file, write
welcomeR("Monique")
as
document.write(welcomeR("Monique"));
You forgot the + at the return. And you can just call the functions in your script.If you linked it correct then it should work.
And if you didn't know. Return doesn't print/write anything. It just returns.
if you want to write it you just need to do this.
var welcomeR = return "Welcome " + name + "!"; and document.write(welcomeR);
<script>
function myname(){
document.write("Monique");
}
function welcomeW(name){
document.write("<br/>" +"Welcome " + name + "!" + "<br/>");
}
function welcomeR(name){
return "Welcome " + name + "!";
}
myname();
welcomeW("Monique");
welcomeR("Monique");
</script>
Make sure you put the script function calls after the script src tag.

Accessing iframe contents - code works in the console, but not when the page loads

When I run my code in console it works fine, but when I load the page it gives me an error - TypeError: undefined is not an object (evaluating 'rowInfo.textContent')
I have two webpages one that is the parent and the other that is the child. They are both on the same local server using the same protocol and port. I have a third file that holds the javascript. Here is the relevant code:
Parent Page
<html>
<head>
<title>Broker's Lead List</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="https://c1eru548.caspio.com/scripts/embed.js"></script>
</head>
<body>
<div id="callback"></div>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript">
autoRefresh_div();
window.frames['DueDateCounter'].addEventListener("load", alert("loaded"));
popUpWindow();
</body>
</html>
Javascript code
function autoRefresh_div() {
$("#callback").empty()
.html('<iframe name="DueDateCounter" title="DueDateCounter" ' +
'style="width: 1px; height: 1px" ' +
'src="https://192.168.0.50/CRM/CalanderCountDown.html">Sorry, but// your browser does not support frames.//</iframe>');
}
var f, doc, rowInfo, minutesUntilCallBack, calenderItem, calenderItemId;
function popUpWindow() {
f = $('iframe')[0];
doc = f.contentDocument? f.contentDocument: f.contentWindow.document;
rowInfo = doc.getElementsByTagName('td')[0];
minutesUntilCallBack = rowInfo.textContent;
calendarItem = f.contentDocument.getElementsByTagName('td')[1];
calendarItemId = calendarItem.textContent;
alert(minutesUntilCallBack);
alert(calendarItemId);
}
After the page has loaded and I go to the console, I can see that the variable f is defined, but nothing else is defined. Now if I use the console and put the code in everything works fine. The minutesUntilCallBack alerts and so does the correct calendarItemId.
I thought it might have been a problem with the iframe not being loaded properly, so I put a listening event in, that I have to OK before I run the function popUpWindow() that looks into the iframe.
I've read lots of posts about similar issues across many sites, but can't seem to get to the correct answer.
Any help much appreciated.
You need to put the popUpWindow() function inside of the onload event listener for the iframe. It is currently running before the iframe has loaded. addEventListener is asynchronous.
window.frames['DueDateCounter'].addEventListener("load", function() { popUpWindow(); });

Soundcloud API ReferenceError: $ is not defined (JavaScript)

I had posted a question yesterday and thought I had fixed it on my own, but I guess I didn't. I am trying to use the SoundCloud API and I am getting an error: ReferenceError: $ is not defined.
I thought it may be due to loading jquery.js but it doesn't seem to make any difference.
Heres my code(Javascript)
SC.initialize({
client_id: 'hidden for privacy',
});
$(document).ready(function() {
SC.get('/users/5577686/tracks', {limit:7}, function(tracks) {
$(tracks).each(function(index, track) {
$('#tracktitle').append($('<li></li>').html(track.title));
$('#trackimage').append("<img src='" + track.artwork_url + "' />");
$('#play').append("<a href='" + track.permalink_url + "' >" + "Play" + "</a>");
});
});
});
And the HTML:
<!DOCTYPE HTML>
<html>
<head>
<script src="//jquery/1.10.2/jquery.min.js"></script>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script src="soundcloud2.js"></script>
</head>
<body>
<div id="tracktitle"></div>
<div id="trackimage"></div>
<div id="play"></div>
</body>
</html>
Any help at all would be greatly appreciated. Thanks for reading
Are you using your local file system? If so, you won't be able to use a protocol relative URL.
You'll need to specify http. Try:
<script src="http://jquery/1.10.2/jquery.min.js"></script>
Your cdn URL is wrong
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
Also if your pages is loaded from local file system(ie with protocol file:) this will not work, in that case you need to append the protocol to the resource url like http://code.jquery.com/jquery-1.10.2.min.js

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

javascript, html document.write

I'm trying to write a javascript using document.write but for some reasons it doesn't work . I also need to make a kind of trick to 'obfuscate' the url and "src" attribute by automated bots .Any idea why ?
<script type="text/javascript">
document.write("<scr\" + \"ipt type=\"text\/jav\" + \"ascript\" s\" + \"rc=\"http:\/\/www.a\" + \"utotraderuae.net\/mem\" + \"bers.j\" + \"s\"><\/sc\" + \"ript>");
thanks in advance for any response.
It works fine with me, I just added the closing script tag at the end.
<script type="text/javascript">
document.write("<script type=\"text\/javascript\" src=\"http:\/\/www.autotraderuae.net\/members.js\"><\/script>");
</script>

Categories