How to simulate target="_blank" in JavaScript

Viewed 373408

When a user clicks on a link, I need to update a field in a database and then open the requested link in a new window. The update is no problem, but I don't know how to open a new window without requiring them to click on another hyperlink.

<body onLoad="document.getElementById('redirect').click">
<a href="http://www.mydomain.com?ReportID=1" id="redirect" target="_blank">Report</a>
</body>
9 Answers

This is how I do it on my website. Severa buttons call one javascript function, which always opens a new tab for each new web page.

<button type="button" onclick="newTab('http://google.com')">search</button>
<button type="button" onclick="newTab('http://amazon.com')">buy</button>
<button type="button" onclick="newTab('http://gmail.com')">read</button>
<script>
    function newTab(url) {
        window.open(url, '_blank');
    }
</script>

changes in js (href='#' target='_blank')

Related