www.webdeveloper.com
Results 1 to 3 of 3

Thread: Using javascript to open search in new tab

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Posts
    8

    Using javascript to open search in new tab

    I have a browser-based search utility with many .php files and many forms. My users select their query parameters from list boxes and submit the form; a new php page comes up that displays their results.

    I've been asked to give my users the option of opening their 'results' page in a new tab in their browser, but my solution isn't really reliable; sometimes it works but sometimes it doesn't. I'm hoping someone can steer me toward a better approach.

    I'm using the onkeydown, onkeyup, and onmouseover events to fire javascript functions. In the Body tags of my HTML, I put this:

    Code:
    <body onkeydown="pageDifferent(event, 'formSites')" 
    	  onkeyup="pageSame('formSites')"
    	  onmouseover="pageSame('formSites');">
    And my javascript functions:

    Code:
    function pageDifferent(pressedKey, formName) {
    	if (pressedKey.keyCode == 18) {
    		document.forms[formName].target='_blank';
    	}		
    }
    function pageSame(formName) {
    	document.forms[formName].target='_self';		
    }
    This works fine about 70% percent of the time, but the rest of the time my users' search results open in the same window. If I remove the 'onmouseover' part, it works all the time - presumably my onmouseover is overriding my onkeydown if it senses a tiny movement. But I need the onmouseover part, or something equivalent, to reset to '_self', so that a new tab only opens while the key is pressed.

    I've tried using anchor tags (CTRL key does what I want by default), but that gives me funky results. I've tried various combinations of onkeydown and onkeypress, but nothing I've come up with does what I want all the time.

    Can anyone point me in a direction that will help? Thanks for any replies.

  2. #2
    Join Date
    Mar 2010
    Posts
    128
    You can try this, I did it using javascript. Its a form that shows your results in a new window if you press the button and have Alt held down (key 18). Anyways, I am guessing this is what you were doing...

    If your having problems with it working differently on different browsers then try jquery, it makes language syntax that can only be interpreted one way... For the most party that is.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Document</title>
            <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
            <script type="text/javascript">
    			$(document).ready(function() {
    				$(document).keydown(function(e){
    					if(e.keyCode==18){
    						$("#formSites").attr("target", "_blank");
    					}
    				});
    				$(document).keyup(function(e){
    					$("#formSites").attr("target", "_self");
    				});
    			});
            </script>
        </head>
        <body>
            <form action="sd.html" method="post" name="formSites" id="formSites" target="_self">
            	<input type="text" value="Keywords..." id="keywords"  /> <input type="submit" id="search" value="Search!" />
            </form>
        </body>
    </html>

  3. #3
    Join Date
    Jul 2010
    Posts
    8
    Proctor, thanks so much for your reply. I'll give this a try. And this may be yet another reason I need to teach myself jquery.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles