1

I have created this code its make to store a table in a variable when i press save and return back to that state when i press restore but i seem to come across a runtime error on the last code (the Id of the table is sudoku) it works in firefox but not IE, Thanks

var clone
function save()
{
    var table = document.getElementById("sudoku")
    clone = table.innerHTML
}

function restore()
{
    document.getElementById("sudoku").innerHTML=clone
}

Edit: Error Message:

Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CPNTDF; .NET4.0E; .NET4.0C; BOIE9;ENUS) Timestamp: Mon, 15 Oct 2012 16:57:44 UTC Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessmen‌​t2/javascript.js Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessmen‌​t2/javascript.js

edit full code:

    var current_cell = null; // the currently selected cell
    var saved = {};     // Object for saving the current game
    function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
for (row = 0; row <=8; row++) {
    for (col=0; col <= 8; col++) {
        var cell = document.getElementById('cell_' + col + '_' + row);
        if (!parseInt(cell.innerHTML)) {
            // cell is empty
            cell.onclick = selectCell;
            cell.className = 'tofill';
        }
    }
}
document.onkeypress = keyPress;
save();
    }
    var current_cell = null; // the currently selected cell
    var saved = {};     // Object for saving the current game
    function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
for (row = 0; row <=8; row++) {
    for (col=0; col <= 8; col++) {
        var cell = document.getElementById('cell_' + col + '_' + row);
        if (!parseInt(cell.innerHTML)) {
            // cell is empty
            cell.onclick = selectCell;
            cell.className = 'tofill';
        }
    }
}
document.onkeypress = keyPress;
save();
    }

    // mouse button event handler
    function selectCell() {
if (current_cell !== null) {
    current_cell.className = 'tofill';
}
current_cell = this;
current_cell.className = 'selected';
    }

    // Capture keyboard key presses. If the key pressed is a digit
    // then add it to the current cell. If it is a space then empty
    // the current cell.
    function keyPress(evt) {
if (current_cell == null)
    return;
var key;
if (evt)
    // firefox or chrome
    key = String.fromCharCode(evt.charCode);
else
    // IE
    key = String.fromCharCode(event.keyCode);
if (key == ' ')
    current_cell.innerHTML = '';
else if (key >= '1' && key <= '9')
    current_cell.innerHTML = key;
    }

    var clone
    function save()
    {
    var table = document.getElementById("sudoku");
    clone = table.innerHTML;
    }

   function restore()
    {
    document.getElementById("sudoku").innerHTML=clone;
   }
Blumer
  • 5,005
  • 2
  • 33
  • 47
  • post the full code.. and the error message.. – Nelson Oct 15 '12 at 16:56
  • Please give the actual error message – itsbruce Oct 15 '12 at 16:56
  • sorry first time using this, the whole code is too much to post is there another way? – jimmy brown Oct 15 '12 at 16:57
  • Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CPNTDF; .NET4.0E; .NET4.0C; BOIE9;ENUS) Timestamp: Mon, 15 Oct 2012 16:57:44 UTC Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessment2/javascript.js Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessment2/javascript.js – jimmy brown Oct 15 '12 at 16:58
  • First guess: The contents of `clone` are getting trashed. – Jeremy J Starcher Oct 15 '12 at 16:59
  • line 50 is document.getElementById("sudoku").innerHTML=clone btw – jimmy brown Oct 15 '12 at 17:03
  • it works on firefox but not IE thats what is bugging me – jimmy brown Oct 15 '12 at 17:03
  • Fact: Anything above an alert will take some debugging in IE! – techfoobar Oct 15 '12 at 17:13

4 Answers4

1

I assume #sudoku is a <table> element, isn't it? Internet Explorer does not allow setting the innerHTML property on table elements.

Instead, use proper DOM methods or just don't try to store HTML strings, but the contents of your sudoku in a two-dimensional array.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

This is a long standing problem with IE and inserting new HTML using the innerHTML attribute.
(Gee, who'd have guessed, a problem with IE!!)

If you're willing to go to jQuery, then you can do it using...

$("#mytable").html(myHtml);

Otherwise it should work if you can somehow place the html into the innerHTML attribute of a <div> tag.

Another option is to create the individual objects yourself using the document.createElement("TR"); style of coding.

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • Fair enough @Jimmy, was just giving you another choice should you have that option – freefaller Oct 15 '12 at 17:21
  • would i just have to replace the entire code with $("#mytable").html(myHtml); ? – jimmy brown Oct 15 '12 at 17:23
  • No @Jimmy, not the entire code... that command is purely for setting the inner html of the selected object, so in your case it would be replacing the `document.getElementById("sudoku").innerHTML=clone` line with `$("#sudoku").html(clone);` – freefaller Oct 15 '12 at 17:26
0

You can use DOM inbuilt javascript method cloneNode to clone your node..

E.G.

 var clone;
    function save()
    {
        var table = document.getElementById("sudoku");
        clone = table.cloneNode(true);
    }

    function restore()
    {
        document.getElementById("sudoku").parentNode.appendChild(clone);
    }

FOR Reference: https://developer.mozilla.org/en-US/docs/DOM/Node.cloneNode

Ashirvad
  • 2,367
  • 1
  • 16
  • 20
  • im afraid that makes a copy instead of removing the first one and replacing it with the previous @Ashirvad – jimmy brown Oct 15 '12 at 17:27
  • it creates exact copy of node.for easy explanation visit http://www.w3schools.com/dom/met_node_clonenode.asp – Ashirvad Oct 15 '12 at 17:29
  • yes , but the copy is created underneath the old table so as a result there is two tables on the browser instead of one – jimmy brown Oct 15 '12 at 17:31
  • then you can just clear the first one like this add this line in restore function document.getElementById("sudoku").parentNode.removeChild(document.getElementById("sudoku")); – Ashirvad Oct 15 '12 at 17:34
-1

The code added by you look like fine but I think you are missing semicolon after each statements.


    var clone;
    function save()
    {
        var table = document.getElementById("sudoku");
        clone = table.innerHTML;
    }

    function restore()
    {
        document.getElementById("sudoku").innerHTML=clone;
    }


Also you can go through jQuery html method. Click here jQuery html() funciton, if you want to read more about html() function.

e.g

    `$('#sudoku').html(clone);`
  • Javascript does not require semi-colons at the end of every statement (unlike C/C++/C#/etc), so this would have no effect on the current issue – freefaller Oct 15 '12 at 17:18