2016-07-05 55 views
0

功能:在文本框中輸入電話號碼(例如:555-555-5555)。文本字段將數字輸出爲平面(由CSS隱藏)。然後,Javascript按ID拾取該數字,並用連字符分開,並將數組分割爲FoneFinder URL搜索字符串,以便在彈出窗口中顯示該網站的結果。彈出窗口正在工作,但正在更改主頁面

問題:彈出窗口工作正常,但是當我點擊鏈接產生鏈接時,它會在主頁面以及彈出窗口中打開鏈接。主頁不應該改變。

彈出式代碼可以在其他頁面上正常工作,並且不會覆蓋主頁面。它必須是JavaScript是如何將html鏈接注入到混淆它的頁面,但我無法弄清楚爲什麼。

任何幫助或見解將不勝感激。

<!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" /> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<style> 

#target_num_result { 
    display: none; 
} 

#target_num_search { 
    font-size: small; 
} 

</style> 


<!-- NewWindow POP UP CODE --> 
<script LANGUAGE="JavaScript"> 

    function NewWindow(mypage, myname, w, h, scroll) { 
     var winl = (screen.width - w)/2; 
     var wint = (screen.height - h)/2; 
     winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable' 
     win = window.open(mypage, myname, winprops) 
     if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); } 
    } 

</script> 


<!-- Script to read the target phone number and split it by hyphens and show a Search link to Fonefinder.net --> 
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){ 
    $('#target_num').on('keyup', function() { 
     var my_value = $(this).val(); 
     $('#target_num_result').html(my_value); 
     var arr = my_value.split('-'); 
     $("#target_num_search").html("&nbsp;&nbsp;<a href=http://www.fonefinder.net/findome.php?npa=" + arr[0] + "&nxx=" + arr[1] + "&thoublock=" + arr[2] + "&usaquerytype=Search+by+Number&cityname= title=FoneFinder onclick=NewWindow(this.href,'FoneFinderLookup','740','680','yes');>!BETA!FoneFinder Search!BETA!</a>"); 
    }); 

});//]]> 

</script> 


</head> 

<body> 

<form id="form1" name="form1" method="post" action=""> 

<table cellpadding="2" cellspacing="0" style="width: 100%"> 
    <tr> 
     <td style="width: 180px">Phone #:</td> 
     <td><label> <input class="text" type="text" name="target_num" id="target_num" /></label><span id="target_num_result"></span><span id="target_num_search"></span></td> 
    </tr> 

    </table> 
<label> 
<input class="button" type="submit" name="submit" id="submit" value="Create" /> 
</label> 
</form> 

</body> 
</html> 
+0

從您提供什麼我不看到一個彈出窗口,只是一個帶有窗體的頁面。你有彈出的例子嗎? – will

+0

問題是當點擊'a'標籤時,瀏覽器進入href的地址。你只想彈出並去href。所以......你應該用'a'鏈接來防止默認動作。只需在'onclick'中加入'return false' –

回答

1

你需要補充的是以下內容:

$('#target_num_search').on('click', 'a', function (event) { 
 
    event.preventDefault(); 
 

 
    var url = $(this).attr('href'); 
 

 
    NewWindow(url,'FoneFinderLookup','740','680','yes'); 
 
})

這樣你可以刪除onclick屬性和移動功能調用JS。看到工作jsfiddle

+0

對不起。但是,這也可以防止'onclick'。你測試過了嗎? –

+0

哦,錯過了一件事=)看看[更新的jsfiddle](https://jsfiddle.net/6ev1vpw7/1/) - 這個想法是完全擺脫onclick參數並移動函數調用到js。這使得代碼稍微更清潔 – dhorelik

+0

感謝您的評論。然而,這是jQuery方式非常好的例子。 (並且仍然沒有jQuery。) –

1

你應該return false爲防止默認行動去鏈接'href'當onlick事件。

(請指出, - 。逗號操作到任何函數返回...它只是砍不使用)

BTW,

<!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" /> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<style> 

#target_num_result { 
    display: none; 
} 

#target_num_search { 
    font-size: small; 
} 

</style> 


<!-- NewWindow POP UP CODE --> 
<script LANGUAGE="JavaScript"> 

    function NewWindow(mypage, myname, w, h, scroll) { 
     var winl = (screen.width - w)/2; 
     var wint = (screen.height - h)/2; 
     winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable' 
     win = window.open(mypage, myname, winprops) 
     if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); } 
    } 

</script> 


<!-- Script to read the target phone number and split it by hyphens and show a Search link to Fonefinder.net --> 
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){ 
    $('#target_num').on('keyup', function() { 
     var my_value = $(this).val(); 
     $('#target_num_result').html(my_value); 
     var arr = my_value.split('-'); 
     var html_tpl = "&nbsp;&nbsp;<a href=http://www.fonefinder.net/findome.php?npa=" + arr[0] + "&nxx=" + arr[1] + "&thoublock=" + arr[2] + "&usaquerytype=Search+by+Number&cityname= title=FoneFinder onclick=\"return NewWindow(this.href,'FoneFinderLookup','740','680','yes'), false\" target='_blank'>!BETA!FoneFinder Search!BETA!</a>"; 
     $("#target_num_search").html(html_tpl); 
    }); 

});//]]> 

</script> 


</head> 

<body> 

<form id="form1" name="form1" method="post" action=""> 

<table cellpadding="2" cellspacing="0" style="width: 100%"> 
    <tr> 
     <td style="width: 180px">Phone #:</td> 
     <td><label> <input class="text" type="text" name="target_num" id="target_num" /></label><span id="target_num_result"></span><span id="target_num_search"></span></td> 
    </tr> 

    </table> 
<label> 
<input class="button" type="submit" name="submit" id="submit" value="Create" /> 
</label> 
</form> 

</body> 
</html> 
+0

謝謝。我試圖做一個返回虛假陳述,但刪除它,因爲它沒有工作。原來我的方法在語法上與你的方法不同。 – noook