Frage

I'm having a problem with javascript. I'm trying to create a button that will cause some content to load over on top of what I got. I created some functions to do this. It works perfectly if I test the function on the HTML page if the script is already loaded, however, when I use an onclick event to call the function, it just creates an entirely new .html page with the content in it.

I know this because I saved the page it created, then I viewed the source, and it was just a blank page with the contents of the javascript function written in the body tag.

Here is the HTML.

            <ul class="sub-menu">
                <li onclick='articleRead("test", DeviantArtUpdateContent)'><a href="#Deviant Art Update">Deviant Art Update</a></li>
                <li><a href="#">'Triumph At Last' Album Preview</a></li>
                <li><a href="#">Short Funny and Stupid</a></li>
                <li><a href="#">Project Preview</a></li>

            </ul>

<div id="aID"></div>

Here is the javascript.

function articlePopUp   ( title , content)  {
document.write('<div class="backgroundFade"></div>');
document.write('<div class="articlePosition"><div class="closeArticle">Close</div>');
document.write('<div class="articleRead">');
document.write('<h1>');
document.write(title);
document.write('</h1>');
document.write('<div class="grey-line"></div>');
document.write(content);
document.write('</div></div>');
}
function articleRead ( title, content)  {
    document.getElementById("aID").innerHTML=articlePopUp(title,content);   
}

Like I said before, if I just call the function in the HTML document without an onclick event it works the way I intended, but adding the onclick event creates a blank document when I click it. I want to write the function into the existing document in the intended div id. Thanks ofr any help in advance, this really gots me stumped.

War es hilfreich?

Lösung 2

document.write will blank out your entire page if it is called after the page loads. You probably want to use innerHTML instead.

function articlePopUp( title , content)  {
    htmlContent = '';
    htmlContent += '<div class="backgroundFade"></div>';
    htmlContent += '<div class="articlePosition"><div class="closeArticle">Close</div>';
    htmlContent += '<div class="articleRead">';
    htmlContent += '<h1>';
    htmlContent += title;
    htmlContent += '</h1>';
    htmlContent += '<div class="grey-line"></div>';
    htmlContent += content;
    htmlContent += '</div></div>';
    return htmlContent;
}
function articleRead ( title, content)  {
    document.getElementById("aID").innerHTML = articlePopUp(title,content);
}

Andere Tipps

After the page has finished loading, it enters a closed state.

Calling document.write on a closed document will implicitly call document.open which will start a new document.

You don't want to add the content to the document inside articlePopUp anyway. You are trying to assign its return value to an innerHTML property. Since there isn't an explicit return value, that is undefined (and the element doesn't exist anyway for the aforementioned reasons).

Have your function build a string and return it.

var html = '<div class="backgroundFade"></div>';
html = html + '<div class="articlePosition"><div class="closeArticle">Close</div>';
// etc
return html;

Better yet, use createElement, createTextNode, appendChild and friends.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top