calling html file from within javascript

40 views
Skip to first unread message

Gary Roach

unread,
Dec 13, 2015, 7:23:11 PM12/13/15
to django-users
I use javascript in my main.html (top template) to control the access
to several buttons. The buttons call a javascript function. This setup
works exactly as I wish except the buttons still don't really do
anything. I need to have each button load a different template. The
pertinent script is:

<script type="text/javascript">
"use strict"
var bn = ""
function buttoncontrol(bn){
var i = 0;
var bx = "";
var x = []; /*array*/
if (bn=="resetDone")
{
for (var i = 0; i < 4; i++)
{
bx= "b" + (i+1);
document.getElementById(bx)
.removeAttribute("disabled");
}
return
}
else {
for (var i = 0; i < 4; i++)
{
bx= "b" + (i+1);
document.getElementById(bx)
.setAttribute("disabled", "true");
}
switch(bn) {
case "b1":
document.write("<a href= 'welcome.html'>'Welcome.html'</a>");
break;
case "b2":
break;
case "b3":
break;
case "b4":
break;
default:
document.write("fell out the bottom");
}
return;
}
}

</script>

The code works till it hits the document.write statement and freezes.
This just plain doesn't work. What would. I want to keep this approach,
so I need an alternative to the document.write statement.

Both my main.html and my welcome.html are in the same template
directory. I bypassed the button during development and the two pages
worked well together.

Django 1.8
Python 3.4

Gary R.

Gergely Polonkai

unread,
Dec 14, 2015, 1:53:28 AM12/14/15
to Django users

Hello,

although this is barelyDjango related, here are some notes.

From MDN[1]: Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically callsdocument.open which will clear the document.

[1] https://developer.mozilla.org/en-US/docs/Web/API/Document/write

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/566E0BC0.8020100%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

Gergely Polonkai

unread,
Dec 14, 2015, 1:55:48 AM12/14/15
to Django users

Sorry, I have pressed Send too soon…

As you see, using document.write is a very bad idea. However, freezing is not mentioned in that document. What does freezing mean in this case? Are you sure it's this code that gets executed and freezes?

Also, what exactly do you want to achieve?

Best,
Gergely

Gary Roach

unread,
Dec 14, 2015, 12:48:06 PM12/14/15
to django...@googlegroups.com
Hi Gergely

I agree that document.write is a bad idea. Freezing in this case may be a poor choice of words. I was trying to convey that the code executes OK until it gets to the .write line and hangs up. It never completes the .write line.

As for purpose. Main.html is a template contains the permanent headers and footers for the site with the body consisting of an iframe containing a {% block content %} {% endblock %} statement. When each button is pushed, all buttons are disabled and the .html template file associated  with the particular button is used to fill the block content. This worked when I didn't use javascript to call welcome.html. But I can't seem to figure out how to serve up the file with javascript. I really don't want to change the general method. I have never liked web sites that allow users to hit the same button twice or hit more than one button in mid load. I know that one can build buttons using link but this seemed more straight forward to me. ie have the  button call a javescript function and let the function do all of the heavy lifting.

I've had many years of various levels of programming and system maintenance but have been retired since 1998 and am pretty rusty. In addition, the technology has shifted significantly since. So for all practical purposes I'm an nooby and am trying to master python, html, javascript and django all at the same time. Learning by doing I guess.

So the bottom line is that I need a code snippet to put under case b1 that will insert welcome.html into the block statement in main.html.

Any help will be sincerely appreciated.

Gary R.
Reply all
Reply to author
Forward
0 new messages