0

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?

1
  • If doing this server-side is a possibility, I would go with that and the target="_blank" option. Commented Aug 22, 2012 at 15:51

2 Answers 2

6
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');"
Sign up to request clarification or add additional context in comments.

1 Comment

If you leave out the dimensions and just put window.open('') then it will open it as a new page/tab.
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.