简体   繁体   中英

Is there a way to display content on same page after a button is clicked?

I am trying to get my content to display on the same page as my button. But when i enter the values and press display the square i am forming is displayed on a new white webpage.

I ask the user to enter two values (height and width), I also ask for a character to form the border of the square, but i have not been able to do that part yet so i just hard coded a # character in for the border in the meantime

What i would like to know is how to display my square on the same page and not in a seperate page.

I use an external JavaScript file: Here is its code. Its named "SquareScript.js":

function Display()
{
var a = document.getElementById("value1").value;
var b = document.getElementById("value2").value;
var outputText = "";

for (var i = 0; i < a; i++)
{
outputText += "#";
}

outputText +="<br>";
for (var r = 0; r < b; r++)
{
outputText += "#";


for(var p = 0; p < a-2; p++)
{
outputText +="&nbsp&nbsp";
}
outputText += "#";
outputText +="<br>";
}

for (var i = 0; i < a; i++)
{
outputText += "#";
}

}

Here is my webpages code:

<!DOCTYPE html>
<html>

    <head>
        <link rel = "stylesheet" type = "text/css" href = "Assignment3.css">
        <style>
            table {background-color:white;color:black;}
            body {background-image: url('squares.png');}
        </style>
    </head>

    <body>
        <div class = "heading">
            <h1><img src = "Interested.png" width = 100px height = 100px></img>WELCOME TO BUILD A   SQUARE</h1>
        </div>

        <script src = "SquareScript.js">
        </script>

        <table>
            <tr>
                <td>
                    Please Enter value number 1:&nbsp&nbsp<input type = "text" id = "value1">
                </td>
            </tr>

            <tr>
                <td>
                    Please Enter value number 2:&nbsp&nbsp<input type = "text" id = "value2">
                </td>
            </tr>

            <tr>
                <td>
                    Please Enter a character:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type = "text"     id = "character"> 
                </td>
            </tr>

            <tr>
                <td>
                    <button onclick = "Display()">Display</button>
                </td>
            </tr>
    <div id="output" style = "background-color:blue;"><script>
     document.getElementById("output").innerHTML(outputText);
    </script></div>

 </table>
    </body>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
</html>

Any useful tips will be appreciated and please consider the fact that I am still new to web development so my code is obviously very basic. Please let me know if you require anything more of me.

Do not use document.write . Create <div id="result"></div> on your page and place your output there. You should create a string variable containing your HTML output. Then you can display this HTML using the following code:

document.getElementById("result").innerHTML = my_html_output;

The way document.write() works is that when it's called outside the HTML page, it automatically creates a new document and writes into that (see the documentation ). This is what's going on in your case: the function is called outside of the HTML (in SquareScript.js) and so it's making a new document, which is the "new white webpage" you're seeing.

You could solve this problem by calling document.write() from within the HTML page. Or you could forego using document.write() and instead reference an element on the existing page (a more flexible solution). By creating a new element in your HTML where the output of your functions should appear (like <div id="output"></div> ), you can use document.getElementById("output") to put your script's output into that element.

You don't need to call this every time you want to add content. Instead, create a new variable to hold your output text as you generate it.

var outputText = "";

Then as you go through your loops, you can add to outputText :

for (var i = 0; i < a; i++) {
    outputText += "#";
}

Then after all your loops are complete, you can insert the content into the output div by making the following function call as the last thing in your display() function:

document.getElementById("output").innerHTML(outputText);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM