/    Sign up×
Community /Pin to ProfileBookmark

Chrome issue with document.write()

the following code when browsed in firefox/MS edge/ IE11 outputs in the browser ” The rate is 1.2345/the rate is 2.0345 etc’)

[CODE]
<script type=”text/javascript”>

function CalDR(){
x = new Date(2007,10,31).getTime();
y = new Date(2012,9,31).getTime();
x1= new Date (2012,10,01).getTime();
y1=new Date().getTime();
var refDate1=document.DR.DOR.value;
refDate=new Date(refDate1).getTime();
var caseTest=0;
if (refDate >= x && refDate<=y) caseTest=1;
if (refDate >= x1 && refDate<=y1) caseTest=2;

switch(caseTest){
case 1:
rate= 1.2345;
// alert (rate);
alert(“rate is “+ rate);
document.write(“<h1>rate is </h1>” + rate);
break;
case 2:
rate= 2.0345;
alert(rate);
document.write (“rate is” +rate);
break;
default: document.write(“exit..exit”);
break;

}
}

</script>
<form name =”DR” >
enter Date of Retirement:<BR/>
<input name=”DOR” type=”date” > <br>

<input type=”submit” value=”Submit” onclick=”CalDR()” >
</form>

[/CODE]

However the same code when run in Chrome browser does not proceed beyond alert( rate ), and does not show the contents inside the document.write (“the rate is “+ rate);where as other browsers show the content inside document.write() in respect of case1 and case2 of the switch statement. Default output inside document.write(exit..exit) is shown in all the browsers including chrome.

what steps can be taken so that document.write() contents are shown in chrome also. If not possible what are the alternatives.
please advise.
thanks
regards
vkwd7

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootMay 08.2016 — document.write can't be used once the document has completed loading.

document.write method is about 20 years out of date.

You update a DOM element with whatever output you have to render in to the document.

So you could have a form element that is read only that will contain the output as updated by your script.

Also, you don't need switch, you just use an if else conditional to set the value of rate. You are doing coding for the sake of it which is also bad programming.

So...

function CalDR( fm ){
date1 = {
x:new Date(2007,10,31).getTime(),
y:new Date(2012,9,31).getTime()
}

date2 = {
x:new Date (2012,10,01).getTime(),
y:new Date().getTime()
}
refDate1 = fm.DOR.value;
console.log("&gt;&gt;&gt; "+refDate1);
refDate=new Date( refDate1 ).getTime();
console.log("&gt;&gt;&gt; "+refDate);
if(refDate &gt;= date1.x &amp;&amp; refDate&lt;=date1.y){
rate=1.2345;
}else if(refDate &gt;= date2.x &amp;&amp; refDate&lt;=date2.y){
rate=2.345
} else rate=false;;

if(rate){
fm.rateCalc.value = rate;
fm.rateCalc.type = "text";
}
}
&lt;/script&gt;
&lt;form name ="DR" action="javascript:;" method="post" onsubmit="CalDR( this );"&gt;
enter Date of Retirement:&lt;BR/&gt;
&lt;input name="DOR" type="date" value=""&gt; &lt;br&gt;
&lt;input name="rateCalc" type="hidden" value="" readonly&gt;
&lt;input type="submit" value="Submit"&gt;
&lt;/form&gt;


should produce what you want. Nothing will show if the date is not in the specified ranges but the chosen rate if it falls in those ranges is displayed.
×

Success!

Help @vkwd7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...