matsko
matsko

Reputation: 22193

target="_blank" with a custom javascript-generated html page

I have a code highlight block for which I would like to have an option where you click on a button and it opens up a new HTML page where it displays the "raw" content of the highlight. I have the code ready in raw form and the link prepared with target="_blank", but I can't seem to get it to open up a new page.

This is what my HTML looks like:

<a href="#" target="_blank">Click to view HTML</a>

And this is my javascript

//when clicked
link.href = 'javascript:document.write("...");';
//the click event should continue as normal

This should open up a new page with "..." as the content, but it doesn't work (it just opens up the existing page).

Is there anyway to do this without using popups?

Upvotes: 0

Views: 582

Answers (2)

kothvandir
kothvandir

Reputation: 2161

Do you have to use a new window? I think is easier to use a layer.

<div id="toggleText" style="border:solid black 1px; display:none;height:100px;width:100px">
<span id="displayText"></span>
</div>


<script language="javascript"> 
func tion toggle() {
    var ele = document.getElementById("toggleText");
    var text = document.getElementById("displayText");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
} 
    </script>

Upvotes: 1

epascarello
epascarello

Reputation: 207511

function writeToWindow(content) {
    var newWin = window.open('','newWin','width=300,height=200');
    newWin.document.open();
    newWin.document.writeln("<html><head><title>Console</title></head><body>" + content + "</body></html>");
    newWin.document.close();
}

call it

onclick="writeToWindow('text to display');"

Upvotes: 6

Related Questions