0

I have a webpage that I display a table full of data from a database. I call this javascript function when I click on the data... in order to change the value... This redirects it back to my mainpage, where I handle adding it to the database...

function confirmCancel(id){
var d = document;
var bReply = confirm('Are you sure you want to cancel Order ' + id + '?');
if (bReply)
    window.event.returnValue = false;
    d.location.href = './main.aspx?action=cancel&orderid=' + id;
}

This would do completely nothing in chrome, until I added window.event.returnValue = false....

I have two other functions I would like to use them in...

function changePriority(id){
var strTemp;
var d = document;

strTemp = prompt('New Priority.  Decrease the value to increase the priority','');
if (strTemp.length > 0 && strTemp.length <= 3 && !isNaN(strTemp)) 
    d.location.href = './main.aspx?action=priority&orderid=' + id +'&priority=' + strTemp;
else
    alert('Invalid Priority Value!');
}

and

function changeQuantity(id, item){
var strTemp;

strTemp = prompt('New Quantity.','');
if (strTemp.length > 0 && strTemp.length <= 3 && !isNaN(strTemp))
    document.location.href = 'viewLoad.aspx?action=quantity&orderid=' + id +'&item=' + item + '&quantity=' + strTemp;
else
    alert('Invalid Quantity Value!');
}

So now, when I add the window.event.return = false; to any other functions... I lose my entire javascript... So all my header/footer disappears... You dont have to look at this code... It is just there to show you how I am writing my header...

function writeHeader (title){
var d = document;
d.write("<tr>");
    d.write("<td width='35%' align='left' valign='bottom' rowspan='2' nowrap class='headerText'>");
        d.write("<img src='./images/mie.jpg' border='0' class='headerLogo1'></img>");
    d.write("</td>");
    d.write("<td width='30%' align='center' valign='top' nowrap class='headerTitle'>");
        d.write(title);
    d.write("</td>");
    d.write("<td width='35%' align='right' valign='center' id='timetext' nowrap class='headerText'>");
        d.write(writeTime());
    d.write("</td>");
d.write("</tr>");
d.write("<tr>");
    d.write("<td align='center' valign='bottom' id='greetingtext' nowrap class='headerText'>");
        d.write(writeGreeting());
    d.write("</td>");
    d.write("<td align='right' valign='bottom' id='datetext' nowrap class='headerText'>");
        d.write(writeDate());
    d.write("</td>");
d.write("</tr>");
d.write("<tr>");
    d.write("<td width='100%' align='center' valign='center' colspan='3' class='headerText'>");
        d.write("<hr color='gray'>");
    d.write("</td>");
d.write("</tr>");
d.write("<tr height='35'>");
    d.write("<td align='left' valign='top' colspan='1' class='headerText1'>");
        d.write("<button name='btnPrint' style='cursor:hand;' class='headerText1' alt='Send this page to the printer' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='self.print();'>Print this Page</button>");
        d.write("<button name='btnEmail' style='cursor:hand;' class='headerText1' alt='Email a link to this page' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='location.href=\"mailto:?subject=A Link from the Component Store Web Site&body=" + escape(location.href) + "%0\A%0\D\";'>Email this Page</button>");
    d.write("</td>");
    d.write("<td align='center' valign='top' colspan='2' class='headerText'>");
        d.write("<table width='100%' align='center' cellspacing='0' cellpadding='0' border='0'>");
            d.write("<tr>");
                d.write("<td width='95%' align='center' valign='center' class=''></td>");
                d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize1' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"smaller\")'>A</td>");
                d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize2' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"default\")'>A</td>");
                d.write("<td align='center' valign='bottom' style='cursor:hand;' class='fontSize3' onMouseOver='this.style.color=\"orangered\";' onMouseOut='this.style.color=\"black\";' onClick='changeStyleSheet(\"larger\");'>A</td>");
            d.write("</tr>");
        d.write("</table>");
    d.write("</td>");
d.write("</tr>");

// Get the routine started that will update the date/time.
setInterval('updateDateTime()', 1000);
}

Any ideas how to fix this issue? Either to have the javascript redirect correctly without window.event.returnvalue = false; or how to fix the issue of the javascript not writing the header/footer when I have window.event.returnvalue = false; in two functions?

Kyle Rickaby
  • 117
  • 4
  • 15
  • 1
    I don't know the answer to your question, but can I just say that you **need** to refactor this code. [document.write](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice), [setInterval eval](http://stackoverflow.com/questions/6081560/is-there-ever-a-good-reason-to-pass-a-string-to-settimeout), [returnValue](http://stackoverflow.com/questions/20045162/event-returnvalue-is-deprecated-please-use-the-standard-event-preventdefault) are all examples of poor coding practices. And all those `.write`s *cannot* be easy to debug – CodingIntrigue Aug 28 '15 at 12:31
  • 1
    What RGraham has said + notice, that all actions which provide server response will block changes to `location.href`, if this response won't be cancelled. These actions are for example clicking a link and submitting a form. – Teemu Aug 28 '15 at 12:38
  • Thank you @Teemu I will check for any of those lingering around – Kyle Rickaby Aug 28 '15 at 12:45
  • So I found out... when I added that second row the reason it didnt work was because I didnt enclose it in from IF statement, which caused an error, which killed the script! – Kyle Rickaby Aug 28 '15 at 12:48
  • @RGraham this is not my code this was written in like 2000 lol I am just trying to make it do a couple of things differently in a timely fashion :D – Kyle Rickaby Aug 28 '15 at 12:49
  • 1
    @KyleRickaby Ah, [Technical Debt](http://martinfowler.com/bliki/TechnicalDebt.html) :) – CodingIntrigue Aug 28 '15 at 12:51

2 Answers2

0

document.location is a read only property and shouldn't be used for redirecting. Instead you should make use of window.location. (https://developer.mozilla.org/en-US/docs/Web/API/Document/location)

Also there is no need of window.event.returnValue.

CuriousMind
  • 3,143
  • 3
  • 29
  • 54
  • From that linked page: *Though Document.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with document.location as if it were a string in most cases: document.location = 'http://www.example.com' is a synonym of document.location.href = 'http://www.example.com'* – CodingIntrigue Aug 28 '15 at 12:36
  • window.location worked for one of the functions.. The very first one... is there something I am missing about that? I am going to run through my code and make sure everything looks the way it should – Kyle Rickaby Aug 28 '15 at 12:44
0

This whole thing could have been avoided if I would have payed more attention to detail...

When I added the new line into my javascript function... I lost all of my javascript because there was a error with that entry... The error was that my silly self forgot to put a bracket around the multiline IF statement... Once I did that, I just added window.event.returnvalue = false; and all was good!

Kyle Rickaby
  • 117
  • 4
  • 15