/    Sign up×
Community /Pin to ProfileBookmark

Create (partial) Webpage with JS – several questions

Hi,
my webpage (draft can be found [URL=http://markstar.sam-city.com/]here[/URL]) basically consists of a table with 2 rows, each with 2 cells. The upper row is just an icon and the title, second row is the menu and the actual information.

I already use an external CSS file to avoid typing the same stuff over and over again and now I would like to do the same with first three cells that are basically the same on every page. My idea was to call a function (tblcreator) that is called when the body is loaded:

[code=html]<body onload=”tblcreator()”>[/code]

Now afaik there are two ways how to add elements with JS:
[B]1)[/B]

[CODE]function tblcreator (){
document.write(“<table><tr><td class=”new1″>This is</td><td>a test.</td></tr></table>”);
}[/CODE]

[B]2)[/B]

[CODE]function tblcreator (){
var myH1 = document.createElement(“h1”);
var myText = document.createTextNode(“This is”);
myH1.appendChild(myText);
var myoutput = document.getElementsByTagName(“body”)[0];
myoutput.appendChild(myH1);
} [/CODE]

Both work but I have severe problems with both approaches:

-> [B]1)[/B]: a) When I successfully call the function tblcreator, it seems I can’t use my external CSS file or other CSS-specs that would affect, for example, the whole document (eg. background-color). Actually it won’t display anything besides what the function writes. Example:

[code=html]<html><head><title>Test</title>

<link rel=”stylesheet” type=”text/css” href=”test.css”>

<script type=”text/javascript”>
function tblcreator ()
{
document.write(“<b>Hi there.</b>”);
document.write(“<table><tr><td class=”new1″>This is</td><td>a test.</td></tr></table>”);
}
</script>
</head>
<body onload=”tblcreator()” style=”background-color:#33FFFF “>
<div style=”background-color:#33FFFF “> This is a test.</div>
</body></html>[/code]

The test.css only consists of this line:

[CODE].new1 { background-color:#00FF00 }[/CODE]

But when you load the page it is still white and the text that is actually in the body is never displayed. Once I disable the function (eg an intentional spelling mistake), the rest works fine. ?

b) While I even get CSS to run with JS, is it possible to make JavaScript-Code work that was [B]created[/B] by JS??? Example (similar to my webpage, obviously requires two gifs in order to work):

[code=html]<html><head><title>Test</title>

<link rel=”stylesheet” type=”text/css” href=”test.css”>
<script type=”text/javascript”>

Normal1 = new Image();
Normal1.src = “newsnor.gif”;
Highlight1 = new Image();
Highlight1.src = “newstmp.gif”;

function changepic(picnr,picobject)
{
window.document.images[picnr].src = picobject.src;
}

function tblcreator ()
{
document.write(“<b>Hi there.</b>”);
document.write(“<table><tr><td class=”new1″>This is</td><td>a test.</td></tr></table>”);
document.write(“<a href=”news.html” onMouseOver=”changepic(0, Highlight1)” onMouseOut=”changepic(0, Normal1)”>”);
document.write(“<img src=”newsnor.gif” alt=”news/main page” style=”position:absolute; top:150px; border:none”><br>”);
}</script>

</head>

<body onload=”tblcreator()” style=”background-color:#33FF22 “>

<div style=”background-color:#F3552F “> This is another test.</div>
</body></html>[/code]

This is the same mousover-mechanism that I use on my page, but when created via JS ist doesn’t work. ? Any idea why or how I could fix this?

2) About the other solution: While I am able to create a singe cell as part of the table, having more than one cell seems to be a problem, disregarding the fact that I have no clue how I would add CSS to that. Here an example:

[code=html]function tblcreator ()
{
var myH1 = document.createElement(“table”);
var myr1 = document.createElement(“tr”);
var myt1 = document.createElement(“td”);
var myt2 = document.createElement(“td”);
var myText = document.createTextNode(“This is a Testpage.”);
myH1.appendChild(myr1);
myr1.appendChild(myt1);
myr1.appendChild(myt2);
myt1.appendChild(myText);
myt2.appendChild(myText);
var outputarea = document.getElementsByTagName(“body”)[0];
outputarea.appendChild(myH1);
}[/code]

But it doesn’t work either. Only the first cell is displayed, the rest disregarded. 😑

Anyways, I think I almost got the first solution to work but I would I would really appreciate some advice on how to:
– Get JS to work within the JS (= OnMouseOver, etc…)
– How to make the rest of the page appear and have a working central CSS file.

Thanks in advance!
Markstar

to post a comment
JavaScript

10 Comments(s) ↴

Copy linkTweet thisAlerts:
@FangJan 29.2006 β€”Β Neither approach is correct, use server-side includes. If JavaScript is disabled the use will not see the full page.

The function calling the document write should be inline; called during the loading of the page. Using document.write after the page has loaded writes a new page with only the contents of the document.write.
Copy linkTweet thisAlerts:
@konithomimoJan 29.2006 β€”Β 
  • 1. document.write will replace all of the code/text already on your page, so don't use it to add to your poage.


  • 2. Just assign an ID and class to the table (the parts in green) you are creating:


  • function tblcreator ()
    {
    var myH1 = document.createElement("table");
    var myr1 = document.createElement("tr");
    var myt1 = document.createElement("td");
    var myt2 = document.createElement("td");
    var myText = document.createTextNode("This is a Testpage.");
    myH1.appendChild(myr1);
    myr1.appendChild(myt1);
    myr1.appendChild(myt2);
    myt1.appendChild(myText);
    myt2.appendChild(myText);
    [COLOR=Green]myH1.id="mytable";[/COLOR]
    var outputarea = document.getElementsByTagName("body")[0];
    outputarea.appendChild(myH1);
    [COLOR=Green]document.getElementById('mytable').className = "new1";[/COLOR]
    }
    Copy linkTweet thisAlerts:
    @MarkstarauthorJan 29.2006 β€”Β Neither approach is correct, use server-side includes. [/QUOTE]I'm sorry but I don't know what this means. :o
    If JavaScript is disabled the use will not see the full page.[/Quote]Yes, I realize that now. ?

    The function calling the document write should be inline; called during the loading of the page. Using document.write after the page has loaded writes a new page with only the contents of the document.write.[/QUOTE] O.k., I think I understand even though I tried to learn more about what inline actually means exactly in this context (for example, I don't see how the one thing is inline and the other one isn't) but my google searches didn't help me.


    1. document.write will replace all of the code/text already on your page, so don't use it to add to your poage.[/quote]Alright, got that. => document.write is of no use to me here.

    2. Just assign an ID and class to the table (the parts in green) you are creating:

    ...[/QUOTE]
    So if I understand correctly, those two green lines enable it to use CSS - great, that solves 33% of my problem, thank you! ?

    Hm, for some reason even the problem with more than one cell is gone so that's that. ?

    But how would I integrate those 2 lines from my original "body" (for the menu or JS in general):

    [code=html]<a href="index.htm" onmouseover="changepic(0, Highlight1)" onmouseout="changepic(0, Normal1)">
    <img src="newsnor.gif" width="130" height="30" border="0" alt="Home"></a><br>[/code]

    Obviously, the image attributes can be put in the CSS-file (should have done that anyways at the beginning :o ). That only leaves the the JS-part: [CODE]onmouseover="changepic(0, Highlight1)" onmouseout="changepic(0, Normal1)"[/CODE]
    This is what I've got so far:
    [CODE]function tblcreator ()
    {
    var mya = document.createElement("a");
    var myhr = document.createAttribute("href");
    myhr.nodeValue = "Test2.html";
    mya.setAttributeNode(myhr);

    var myim = document.createElement("img");
    var mysr = document.createAttribute("src");
    mysr.nodeValue = "newsnor.gif";
    myim.setAttributeNode(mysr);

    mya.appendChild(myim);

    var Ausgabebereich = document.getElementsByTagName("body")[0];
    Ausgabebereich.appendChild(mya);
    }[/CODE]
    It creates a link with an image, positioning etc can be done via CSS but I have no clue on how to make the "onmouseover/onmouseout"-part work. ?

    And another problem remains: I can create a table now with 4 cells, great, but how do I assign the text that is actually supposed to be in the body to be assigned to a specific (the 4th) cell??? If I take the code from the last post and add text, it gets displayed [B]before[/B] the table. [B]Is there a way to integrate [i]HTML[/i] into the table created by JS?[/B]

    The only way I can think of is put the text in a "div" and use CSS to move it so it overlaps with the other table. Doesn't sound like a very 'professional' solution, though (and I'm a perfectionist).

    Thank you for the help so far and for sticking with me! ?
    Copy linkTweet thisAlerts:
    @FangJan 29.2006 β€”Β document.write:&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="test.css"&gt;
    &lt;/head&gt;
    &lt;body style="background-color:#33FFFF"&gt;
    &lt;script type="text/javascript"&gt;
    document.write("&lt;b&gt;Hi there.&lt;/b&gt;");
    document.write("&lt;table&gt;&lt;tr&gt;&lt;td class="new1"&gt;This is&lt;/td&gt;&lt;td&gt;a test.&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");
    &lt;/script&gt;
    &lt;div style="background-color:#33FFFF"&gt;
    This is a test.
    &lt;/div&gt;
    &lt;/body&gt;
    &lt;/html&gt;

    [URL=http://www.webreference.com/programming/ssi/intro/]server-side include[/URL]

    [URL=http://www.webreference.com/programming/phpanth1/5.html]PHP include[/URL]
    Copy linkTweet thisAlerts:
    @konithomimoJan 29.2006 β€”Β The table now has an ID of 'mytable'.

    You can now use JS or CSS to access it. Just use document.getElementById('mytable') to access it and then change whatever you want.

    For example, if you want to affect the fourth cell you would do this:

    var tables = document.getElementById('mytable');

    var cells = tables.getElementsByTagName('td')

    cells[3].innerText = 'Whatever you want it's text to be!!!';

    I used 3 because cells is an array(list) of all cells located within mytable. Arrays start counting at 0, so cells[3] is actually the fourth cell.
    Copy linkTweet thisAlerts:
    @MarkstarauthorJan 30.2006 β€”Β @Fang: Thank you. The problem here is that now I have to write the whole page in the 'body' again, is that correct?

    @konithomimo: Thanks, while I thought of that as well, the "problem" with that is that my whole page is in JS then which means that whenever I write a text and what to use quotation marks, I have to write " and so on.

    Now that I thought about it for quite a while I think I will use your approach for the first 3 cells which I will position exactly via CSS (top:...px; left:...px; ...) and use a 'div' for the last "cell" (which will superimpose via CSS). It's interesting to test around with this since the HTML in the body is loaded first, then the JS-part is briefly displayed [B]after[/B] the HTML before it gets moved to the right position. ?

    Also, could you please give me an example on how I would add JS to 'mytable' (or better yet for the hyperlink-example from my last post which would have the additional line of [CODE]mya.id="myhyperlink";[/CODE])? I understand how to add more child elements and CSS, but JS? ?

    Oh, and btw, just for the record; this is what I originally had in mind:
    [code=html]<html><head>
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="test.css">
    <script src="test.js" type="text/javascript"></script> <-- tblcreator() and other functions -->
    </head>

    <body onLoad="tblcreator()"> <-- creates the first 3 (4) cells -->
    <-- Rest of the HTML-code that belongs in cell[3] in HTML and not JavaScript! -->
    </body>
    </html>[/code]
    Anyways, I come to realize that maybe I should have
    Copy linkTweet thisAlerts:
    @FangJan 30.2006 β€”Β Don't use the onload to run functions using document.write&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Example&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="test.css"&gt;
    &lt;script src="test.js" type="text/javascript"&gt;
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;script type="text/javascript"&gt;
    tblcreator();
    &lt;/script&gt;
    &lt;!-- Rest of the HTML-code that belongs in cell[3] in HTML and not JavaScript! --&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @MarkstarauthorJan 30.2006 β€”Β Yes, indeed, loading the "tblcreator()" from within the body seems to be the better solution, thanks. Now only one thing remains, I apologize for being stupid - but how do I get the [B]"onmouseover=..."[/B] to work? All I found was those five options that I could assign: className/dir/id/lang/title. ?

    Edit: I had an idea but it didn't really work. My approach: Assign 'mya' a javascript-attribute, taken from this example: [CODE]<a href="javascript:somefunction()">Some text.</a>[/CODE] Here is my approach (added the green part):&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;&lt;html&gt;&lt;head&gt;
    &lt;title&gt;Example&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="test.css"&gt;
    &lt;script type="text/javascript"&gt;

    Normal1 = new Image();
    Normal1.src = "newsnor.gif";
    Highlight1 = new Image();
    Highlight1.src = "newstmp.gif";

    function changepic(picnr,picobject){
    window.document.images[picnr].src = picobject.src;
    }

    function tblcreator (){
    var mya = document.createElement("a");
    var myhr = document.createAttribute("href");
    myhr.nodeValue = "Test2.html";
    mya.setAttributeNode(myhr);
    mya.id="mytry";

    <i> </i>var myim = document.createElement("img");
    <i> </i>var mysr = document.createAttribute("src");
    <i> </i>mysr.nodeValue = "newsnor.gif";
    <i> </i>myim.setAttributeNode(mysr);

    <i> </i>// My idea on how it might work...
    [COLOR=Green] var myjs = document.createAttribute("javascript");
    myjs.nodeValue = "onmouseover=changepic(0,Highlight1)"";
    mya.setAttributeNode(myjs);[/COLOR]
    // ... but it didn't. :-(

    <i> </i>mya.appendChild(myim);

    <i> </i>var Ausgabebereich = document.getElementsByTagName("body")[0];
    <i> </i>Ausgabebereich.appendChild(mya);
    <i> </i>document.getElementById('mytry').className = "new1"; // Assign CSS
    }

    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    Rest of the HTML-code that belongs in cell[3] in HTML and not JavaScript!&lt;script type="text/javascript"&gt;tblcreator();&lt;/script&gt;
    &lt;/body&gt;&lt;/html&gt;

    Sadly, no change and I'd still like to get it to work otherwise this whole thing was for nothing unless there is another way to implement the mouseover to a specific area. :o
    Copy linkTweet thisAlerts:
    @FangJan 31.2006 β€”Β myjs.onmouseover=function(){changepic(0,Highlight1);};
    Copy linkTweet thisAlerts:
    @MarkstarauthorJan 31.2006 β€”Β [SIZE=7][B]Thank you!!![/B] [/SIZE]
    Γ—

    Success!

    Help @Markstar 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.19,
    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,
    )...