Jump to content

how to launch src/main.js in index.html with addEventListener ?


espace
 Share

Recommended Posts

hi,i want to use cordova so i need to implement this function :

document.addEventListener("deviceready", myfun, false);

function myfun(){//my code}

now i have my main.js in a separate file where i want to place my function "myfun" so in my index.html  i write this :

<!DOCTYPE HTML>
<html>
	<head>
		<title>Papermania</title>
		<link rel="shortcut icon" href="assets/favicon.png" type="image/x-icon" />
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="chrome=1, IE=9">
		<meta name="format-detection" content="telephone=no">
		<meta name="HandheldFriendly" content="true" />
		<meta name="robots" content="noindex,nofollow" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-mobile-web-app-status-bar-style" content="black" />
		<meta name="apple-mobile-web-app-title" content="Phaser App">


		<script src="phaser.min.js"></script>
<script src="main.js"></script>		

		<script type="text/javascript">
document.addEventListener("deviceready",myfun, false);
</script>

<style>
	body {
		
		margin: auto;
		display: table;
		position: absolute;
		border:0px;
		top: 0px;
		left: 0px;
		padding: 0; margin: 0;
		background-color: #1a1a1a
	}
</style>
	</head>
	</body>
</html>

but that don't work because my index.html file don't recognize the "myfun" function. how do you do to perform that ?

in addition i will say that i know how to perform this action with multiple files and bootstate but in my case i want that all of my snippets were on a single file (main.js).

thanks for your help.

Link to comment
Share on other sites

If you only run the myfun function once - when the document loads then you could just make it a self-executing function by putting () after the function.

 

Alternatively you could put a function in your main.js that self-executes for the event listener.

 

function runonce(){
document.addEventListener("deviceready", myfun, false); 
}()

function myfun(){//my code}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...