I have made a page in javascript and calls a function in his onload, but that didn't called if its loaded from hlink, but call that function if reload the page, any help will be appreciated.
i have a function name loadbody in external Js file here is my function
function loadbody(){
alert("Get values from local storage");
}
i write this function in doument.ready() . here is my index page
<body onload="loadbody()">
<h1> this is phonegap indexpage </h1>
</body>
Just use a onclick event in hlink tag :)
here is your HTML page ::
<!DOCTYPE html>
<html>
<head>
<title>Js </title>
<script src="script.js"></script>
</head>
<body>
<h1> this is phonegap indexpage </h1>
<a href='http://www.google.com' onclick='loadbody();'>mylink</a>
</body>
</html>
Here is your external JS page ::
function loadbody(){
alert("Get values from local storage");
}
Related
I have the following html code:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Soemething</h1>
<button id='btn'>Take me to Google</button>
<script>
document.getElementById('btn').addEventListener('click', doStuff);
function doStuff() {
window.location.assign("https://www.google.com/");
}
</script>
</body>
</html>
How do I wait for Google to load before executing code on google.com such as doing a search?
Thank you
Your script's sandbox is only the page whether it is executing. Once, you are redirected to google.com, your script will become invalid.
Your only option is to get the page via AJAX and run a callback function to execute when the content is loaded.
the code is somewhat like
<body onload="testFunc();">
<div></div>
<script type="text/javascript" src="/js/test.js"></script>
</body>
and the testFunc is inside test.js
and sometimes its not getting called
The onload attribute fires when an object has been loaded.
onload is most often used within the element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("body loaded");
}
</script>
</head>
<body onload="myFunction()">
<h1>body loaded!</h1>
</body>
</html>
page1.html
<!DOCTYPE html>
<html>
<head>
<script src="common_script.js"></script>
</head>
<body onload="init()">
<h1>My First Page</h1>
<p id="demo">This is a paragraph.</p>
</body>
</html>
page2.html
<!DOCTYPE html>
<html>
<head>
<script src="common_script.js"></script>
</head>
<body>
<h1>My second Page</h1>
<div id="nextpageId">I want to access my div</div>
</body>
</html>
common_script.js
function init() {
alert($("#nextpageId").text());
}
In the following code I have two pages i.e. page1.html & page2.html and these contain the common JavaScript file.
My problem is to access page2.html div#id when page1.html is loading.
use this in your javascript
$('body').attr('unique_id')
and give unique id for your page in body tag
<body id='unique_id'>
or you can also pass in url
or use this code
function init() {
alert( $('div').attr('id') == "nextpageId" )
}
You don't explicitly state that you are using jQuery Mobile but you have tagged your question as such and are using PhoneGap so I will assume so...
Your pages don't conform to a standard jQuery Mobile layout. They are missing the required data-roles. Specifically data-role="page" along with child data-role="content" declarations.
http://jquerymobile.com/demos/1.2.0/docs/pages/page-anatomy.html
Your second page will be loaded via Ajax and the <script> tag on the second page will be ignored. In fact, only the contents of the nested <div data-role="page"> section will be loaded.
To pull that markup, you can simply access the <div> given its ID and that ID should be unique. You can use the pageinit or pageshow events to control your timing on the Ajax load.
http://jquerymobile.com/demos/1.2.0/docs/api/events.html
Essentially, all I want to do is open an external web page after the current page is loaded via java script.
open my page -> javascript tells browser to open external page -> external page being loaded into the broser
How may I accomplish this?
you may use this
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function load()
{
window.location.href = "http://externalpage.com";
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>
Hope it should be window.location. Check the code.
Technically you can:
location.href = "http://example.net/";
… but you should perform an HTTP redirect instead as that is more reliable, faster and better food for search engines.
You can also use the "open" method to open the source file or url on a new window.
window.open("anyfile.*");
or
window.open("http://anylocation.com");
Hi please try this code for page loading time we will redirect whatever u configured the urls.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
window.open('https://google.com');
}
</script>
</head>
<body onload="myFunction()">
</body>
</html>
<body>
<script>
document.body.innerHTML += 'Link';
document.getElementById("link").click();
</script>
<body>
I am trying to access a JavaScript function in a programatically created iFrame from a JavaScript function outside. I tried several ways, but was not successful. L ike window.frames['frameid'], etc.
Could you please provide me the correct syntax?
Yes, they are in the same domain. Actually, I am using ExtJS framework. I have a JSP inside an iFrame, I would like to call the javascript functions in the JSP from the a javascript function outside which is basically in a JS file.
Here's a more appropriate example based on your comments ;)
The main page
<html>
<head>
<title>Main Page</title>
</head>
<body>
<iframe src="iframe.html"></iframe>
<script>
var receiver = {
listen:function(msg){
alert(msg);
}
};
</script>
</body>
</html>
The iframe page: iframe.html, but can be a JSP with similar output
<html>
<head>
<title>iframe page</title>
<script src="external.js"></script>
</head>
<body>
<!-- some content here -->
<script>
externalFunction('hello', window);
</script>
</body>
</html>
And the JS file: external.js
function externalFunction(msg, w){
w.parent.receiver.listen(msg);
}
Place those 3 files in the same directory and open the main page.
You should get a popup with "hello".