0

I am struggling with the following problem.

I have made a memorygame with javascrpt for school.It all works fine, but my teacher told me that i can not have on line of javascript in my HTML, like this :

HTML :

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="styles.css">
        <title>Memory spelen</title>
    </head>
    <body>
        <script type="text/javascript" src="javascript.js"></script>
        <div id="memory_board"></div>
        <script>newBoard();</script>
   </body>
</html>

The newBoard() is applied to the memory_board div. How can i take this little piece of script out of my HTML file and place it in my js file, to still function properly.

Thanks in advance

2
  • What does newBoard do? Can you create a jsfiddle.net for us? Commented Jun 23, 2014 at 7:24
  • that piece of code will work like it was doing in your html if you don't jave server generated ids... else you will need a polyfill for those ids Commented Jun 23, 2014 at 7:26

4 Answers 4

2

Inside of your javascript.js put this

window.onload = function {

    // content of your script

    var newBoard = function(){
        // the new updated newBoard() function from below
    }

    // other parts of your script

    if(window.location.href == 'your-url') {
        // now, after the newBoard() has been updated
        // the next to lines are not needed 
        // var board = document.getElementById('memory_board');            
        // board.innerHTML = newBoard();
        // just call the function
        newBoard();
    }
};

UPDATE

I just took a look at your old fiddle and I changed your newBoard function to this

function newBoard(){
    tiles_flipped = 0;
    var output = '';
    memory_array.memory_tile_shuffle();
    for(var i = 0; i < memory_array.length; i++){
        var div = document.createElement('DIV');
        div.id =  "tile_" + i;
        (function(div,i){
            div.onclick = function() {
                memoryFlipTile(this, memory_array[i]);
            };
        }(div,i));
        document.getElementById('memory_board').appendChild(div);
    }
}

Check the working fiddle.

Sign up to request clarification or add additional context in comments.

6 Comments

but what if you don't want this to run on each page? and you insert that js in a template layout?
Thanks, the problem still is that my javascript code doesn't apply to the <div>memory_board</div> in my HTML.
jsfiddle.net/2GW3k/5 this is my full code. If you like, please check it. Would be extremely helpful!
@hex494D49 this is better ;) ... all the time cases like this can appear so you should better be prepared for it
@HellBaby Check the updated answer; now you have a working program / game :)
|
1

try to put it like this in external js file

$(document).ready(function(){
   newBoard();
});

1 Comment

he want to do it on load of particular div. add filter to follow his requirement
1

It looks that you need to call newBoard() method on onload event of memory_board div , You can do this in following ways:

<div id="memory_board" onload="javascript:newBoard()"  ></div>  // use onload event of memory_board

Comments

0

you can use onload function on the javascript. It will call the function when all the HTML tag is loaded on the screen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.