I am trying to start opera from command line and redirect it to a page in 30 seconds. What I'm trying now is:
C:\Programme\Opera\opera.exe -newpage javascript:function%20func1(){window.location.href='http://localhost/';}setTimeout('func1()',30000);
Which is returing a page with as content "1". Func1 is never called. Is there a good way to solve this inline? Or should I create a page with this content?
Just found a better solution:
1.) change the command line to:
C:\Programme\Opera\opera.exe -newpage file://localhost/C:/redirect.html
2.) create the file redirect.html with the code for a redirect:
<html>
<head>
<title>Startup</title>
<script>
function redirect()
{
window.location.href = 'http://localhost/startup.php';
}
setTimeout('redirect()',60000);
</script>
</head>
<body>
<p>Loading...</p>
</body>
</html>
Regarding the original example, you should have put void() around the setTimeout() call. setTimeout() returns a timeout identifier (in this case the number 1) which becomes the output of the javascript: URL. Whatever a javascript: URL 'returns' is used as the source for a document.
Related
I am making a web dashboard and for this I have a javascript function that loops infinitely, updating a particular div at frequent intervals. (it uses ajax/jquery to do this).
I have used a loop with setTimeout:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
function updateMain() {
setTimeout(function() {
console.log("updating");
$('#main').load('new-content.php #main', function() {});
updateMain();
}, 3000)
}
updateMain();
</script>
</head>
<body>
<div id="main">
<p>hi there</p>
</div>
</body>
</html>
'new-content.php' is a file that is constantly updated by another part of my program, to show the new content for the dashboard.
it looks something like this:
<div id="main">
<p>Hello i am updated content</p>
</div>
I am sure the loop is working as the "updating" messages appear at regular intervals in the console.
When the program starts, the div is showing "hello there", and in the new-content.php file it has "hello am updated content".
The first time the loop runs, it updates the div to show "hello i am updated content".
But if I further update the new-content.php file, for example to say "hello I am further updated content", it just won't show on my webpage. However I am sure that the loop is still running as the messages appear in console.
It's like ajax has some cached version of the new-content.php file that it loads at the start then keeps using forever.
I am very confused, if you could help me I would be very grateful
Thank you
PS: if it is of relevance, the website is running through flask but I don't think this is the problem.
EDIT: I believe it is actually a flask caching issue
You can try adding a cache buster to see if that helps. Change your code as follows:
function updateMain() {
setTimeout(function() {
console.log("updating");
const cb = new Date().getTime();
$('#main').load('new-content.php?cb=' + cb + '#main',function() {
updateMain();
});
}, 3000)
}
updateMain();
Wasn't able to solve the issue but for anyone who is trying to make a similar project, I decided to use flask-socketio instead which allows me to send data to javascript to be shown on the page, instead of javascript loading the data itself from a file. (the answer by vladtn on this SO post was useful as an introduction to socketio)
This question already has answers here:
javascript <script> tag - code execution before src download
(4 answers)
Closed 8 years ago.
I am relatively new to JavaScript so this might be somewhat trivial. However I can't seem to find the answer to this question.
Say I have a JavaScript file (bar.js) with a function in it called foo(). I want to call this function (foo) inside a script tag. I would like it to work like so.
<script type="text/javascript" src="bar.js">
foo();
</script>
I am not able to get this to work. I have ran the JavaScript console with my browser and what it seems to be doing is...nothing. No syntax errors or anything.
I can run a function similarly with a button click...using the script tag above and this.
<button type="button" onclick="foo();">Click Me</button>
I could do it this way, but in the actual circumstance I need to pass parameters into the function that is being called on the button click. I can't get those recognized either. I'm sure that something to do with scope.
The way I tried this was like so...
<script type="text/javascript" src="bar.js">
var a = "blah";
var b = "blab";
</script>
.... (some more html)
<button type="button" onclick="foo(a,b);">Click me </button>
Here I get that a is undefined. Which leads me to think that it is a scope problem. The script tag is in the head section and the button is in the body section. Can you put script tags outside of the head and body tags to make global data?
Thanks for the help in advance.
I have never used jsfiddle before and was having trouble getting it to work so I'll just post and example code here.
<html>
<head>
<script type="text/javascript" src="bar.js">
</script>
<!--From what yall say I should have another script
tag here for anything else. Say some variable?-->
<script type="text/javascript">
var a = "hello";
var b = "text";
</script>
</head>
<body>
<!--This should work now?-->
<button type="button" onclick="foo(b,a)">
Click me
</button>
</body>
</html>
bar.js contents:
function foo(id,string){
document.getElementById(id).innerHTML = string;
}
I got this to work.
Thanks everyone.
You need to first include the javascript containing the function:
<script type="text/javascript" src="bar.js"></script>
and then call it in another script tag:
<script type="text/javascript">
foo();
</script>
In your example you seem to have mixed 2 notions into a single script tag which is invalid: include an external javascript file and in the body of the script tag write your code.
According to the specification:
The script may be defined within the contents of the SCRIPT element or
in an external file. If the src attribute is not set, user agents must
interpret the contents of the element as the script. If the src has a
URI value, user agents must ignore the element's contents and retrieve
the script via the URI.
So basically you should avoid such situations and have separate script tags for including external files and for writing inline js.
On the first page:
<button type="button" onclick="myFunction()">click</button>
Then on another page:
myFunction() {
alert("working");
};
This is the receiving page.
all the basic tags
<script>
function alert() {
alert("The Link Has Been Successful");
}
</script>
The question is: How do I call the other function on the other page to alert that page <button onclick="????">?
<body>
<button onclick="???" type="button">Alert on the other page</button>
</body>
Call a function in one page when the function is on another page
That is not possible. The definition of said function must be present on the current page for it to be executed.
The standard approach is to extract your function into it's own (or global) JavaScript file. Then reference it on pages that need to use it.
You can place all your global functions etc in a .js text file and then on all pages that need to call any of these routines you should declare it like this...
<script src="myglobals.js" type="text/javascript"></script>
Now you can access your global functions and variables. Good luck!
You do not need to link 2 pages together you just need to link them to your index.html by using: and then js will do all the work for you!
Hope This Helps!
LucaSpeedStack
I have Js Src code that contain Jsonp api functions. Where ever this Js Src code got printed i got to check first if the "api tag" printed already , if it did -> execute the api function , if the "api tag" have not got printed yet , wait for the tag to get printed on the screen and then execute the api functions.
for example i have this code
<html>
<head>
<!-- js code - contain Jsonp functions -->
<script type="text/javascript" src="jsonp.js"></script>
</head>
<body>
<!-- when this newtag get print , the function starts -->
<newtag:api size="small">myNewTag</newtag:api>
</body>
I don't know where the user will decide to put the JS code , it may be in the head , body or in the middle of the page , since i have this mystery i cant do something like this :
<newtag:api size="small">myNewTag</newtag:api>
<script> startJsonp(); </script>
because if the js code did not printed yet the order will not be execute.
and if the Js code got print in the head so it will not work since the "api tag" did not got printed yet.
to make the api work i need the "api tag" to be printed on the screen already for the Jsonp functions to work.
what i can do to solve this?
what is the porpoise to write JS function like that?
(function(){
// do something
})();
thank you so much!
There are several solutions for this problem:
Using jQuery's ready function. E.g.:
jQuery.ready(function(){ startJsonp(); })
Using window.onload. The problem is that you can unintended override another window.onready function. E.g.:
window.onready = startJsonp();
Using document.addEventListener and document.attachEvent. Eg.:
if (document.addEventListener){
document.addEventListener('load', startJsonp, false);
} else if (el.attachEvent){
document.attachEvent('onload', startJsonp);
}
Here is my problem - I'm trying to write a self-updating application, but I keep getting an error saying that runtime.air.update.ApplicationUpdaterUI() does not return a constructor.
Here's the relevant section of the code; there are other javascript files being included, but I don't think that any of them would be actively breaking AIR itself.
<html>
<head>
<script type="text/javascript" src="./js/AIRAliases.js"></script>
<script type="text/javascript" src="./js/jquery-1.3.1.js"></script>
<script src="ApplicationUpdater_UI.swf" type="application/x-shockwave-flash"></script>
<script>
$(document).ready( function() {
var appUpdater = new runtime.air.update.ApplicationUpdaterUI(); // line 64 in this example
}
</script>
</head>
<body> ... stuff ... </body>
</html>
And the error that I get when I test it is
TypeError: Value is not a constructor. Cannot be used with new.
at app:/index3.html : 64
at app:/js/jquery-1.3.1.js : 2912
at app:/js/jquery-1.3.1.js : 686
at app:/js/jquery-1.3.1.js : 2916
at app:/js/jquery-1.3.1.js : 2936
Verify the path in that script tag for the SWF, I'm guessing you do not have the reference to the ApplicationUpdater_UI.swf correct.
Air is basically complaining that it cannot find a runtime.air.update.ApplicationUpdaterUI() method to call anywhere, which likely means it can't find the SWF (or I suppose it's possible the SWF is corrupted).
Not sure if this is related, but neither $(document).ready nor .load will work if you load the .swf before the script. Make sure you put the .swf reference at the very bottom of your page.