2011-09-15 63 views
0

我使用的是Google最初提供的修改後的代碼,用於製作使用Google地圖的轉乘行程規劃表單。我希望結果能夠在新的窗口/標籤中打開,這樣用戶就不必離開網站(我意識到這是一個有點爭議的可用性問題,但這是我的客戶的要求,所以......)。在新窗口中打開動態構建的Google URL

下面的代碼在標籤中使用了target =「_ blank」 - 這看起來像是最簡單的解決方案 - 但在這種情況下不起作用 - 不知道爲什麼,但認爲它可能與動態方法有關,內置網址。

我試圖從這個網站以前類似的問題收集了幾種方法,下面就是其中的一種方法,以及沿

myForm.setAttribute("target", "_blank"); 

但至今沒有任何東西線已經成功。

這是我正在使用的代碼。任何建議將不勝感激。

 <script language="JavaScript" type="text/javascript"> 
// Edit the strings below this line 
var startExample = "USC"; 
var endExample = "201 N Los Angeles St."; 
var zip = "90012"; 
// End edit block 

// Get and parse the user's current date/time 
var date = new Date(); 
var isPM = false; 
var currentTime = date.getHours(); 
var currentDate = ""; 
var amOption = '<option value="am">AM'; 
var pmOption = '<option value="pm">PM'; 

if(currentTime > 11) 
isPM = true; 
currentTime %= 12; 
if(currentTime == 0) 
currentTime = 12; 

currentTime += ':'; 
if(date.getMinutes() < 10) 
currentTime += '0'; 
currentTime += date.getMinutes(); 

if(isPM) 
pmOption = '<option selected="true" value="pm">PM'; 
else 
amOption = '<option selected="true" value="am">AM'; 

if(date.getMonth() < 9) 
currentDate = '0'; 
currentDate += (date.getMonth() + 1) + '/'; 
if(date.getDate() < 10) 
currentDate += '0'; 
currentDate += date.getDate() + '/' + date.getFullYear(); 

// Builds the destination URL 
function buildURL() { 
var loc = 'http://www.google.com/maps?ie=UTF8&f=d&'; 

for (var i = 0; (i < document.myForm.length - 1); i++) { 
if(document.myForm[i].name == 'ampm') 
continue; 
if(document.myForm[i].name == 'time') 
loc += document.myForm[i].name + '=' + document.myForm[i].value + document.myForm.ampm.value + '&'; 
else { 
if(document.myForm[i].name == 'saddr') 
loc += document.myForm[i].name + '=' + document.myForm[i].value + '+near+' + zip + '&'; 
else 
loc += document.myForm[i].name + '=' + document.myForm[i].value + '&'; 
} 
} 
loc+='dirflg=r'; 


location.href=loc; 
return true; 


} 
</script> 

<form name="myForm" id="myForm" action="#" target="_blank"> 

    <p>Powered by <a href="http://google.com/transit" target="_blank">Google Maps</a></p> 
    <label for="saddr"><strong>Start</strong> e.g. 
    <script language="JavaScript" type="text/javascript"> 
    document.write(startExample) 
    </script> 
    </label> 

    <input type="text" name="saddr" maxlength="2048" title="Enter the Origin Address" class="startend" /> 

    <label for="daddr"><strong>End</strong> e.g. 
     <script language="JavaScript" type="text/javascript">document.write(endExample)</script></label> 

     <input type="text" name="daddr" maxlength="2048" title="Enter the Destination Address" class="startend" /> 
    <div class="gadgetform-row"> 
<script language="JavaScript" type="text/javascript"> 
document.write('<div class="datewrapper">'); 
document.write('<label for="date"><strong>Date</strong></label><br />'); 
document.write('<input class="date" type="text" id="fdate" name="date" value="' + currentDate + '" maxlength="10" title="Enter the Date in MM/DD/YY format">'); 
document.write('</div>'); 
document.write('<div class="timewrapper">'); 
document.write('<label for="time"><strong>Time</strong></label><br />'); 
document.write('<input class="time" type="text" id="ftime" name="time" value="' + currentTime + '" maxlength="8" title="Enter the Time in HH:MM AM or PM format">'); 
document.write('</div>'); 
document.write('<div class="ampmwrapper">'); 
document.write('&nbsp;<br />'); 
document.write('<select name="ampm" class="ampm">' + amOption + pmOption + '</select>'); 
document.write('</div>'); 
document.write('<div class="clear"></div>'); 
</script> 
</div>  

<div class="planby"> 
    <label for="ttype"><strong>Plan by:</strong>&nbsp;</label 
><select name="ttype"> 
    <option value="dep">Departure Time </option> 
    <option value="arr">Arrival Time</option> 
    </select> 
</div> 

<a class="button" href="javascript:void();" onclick='return buildURL();'><span class="plan">Plan My Trip</span></a> 
</form> 

回答

2

您可以嘗試使用window.open來代替。下面是我打開的子窗口中創建一個函數:

openChildWindowWithDimensions = function(url, width, height, showMenu, canResize, showScrollbars) { 
    var childWindow = window.open(url, "", "\"width=" + width + ",height=" + height + ",menubar=" + (showMenu ? "1" : "0") + ",scrollbars=" + (showScrollbars ? "1" : "0") + ",resizable=" + (canResize ? "1" : "0") + "\""); 
    if (childWindow){ 
     childWindow.resizeTo(width, height); //IE9 dimensions bug 
    } 
} 

添加此功能到您的網頁,並在你的功能是建立在URL末尾,這樣稱呼它:

openChildWindowWithDimensions(loc, 800, 600, true, true, true); 
+0

downvote?我很抱歉@詹姆斯,我不知道那是什麼(這裏還是新的),我甚至沒有點擊頁面上的任何內容,我敢肯定我無論如何都沒有讓你失望。你在跟別人說話嗎? – Halfacre

+0

我很漂亮的JavaScript天真...以及如何將這工作到我現有的代碼的任何提示?謝謝你的提示! – Halfacre

+0

@Halfacre:編輯我的答案。 –