2012-03-27 24 views
1

我在我的wp7應用程序中使用javascript函數時遇到問題。我基本上試圖創建一個Find on Page按鈕,就像在Windows Phone的默認IE瀏覽器中看到的一樣。到目前爲止,我已經引用了一個來自http://www.liveside.net/2011/10/21/tip-how-to-get-forward-and-find-on-page-back-in-ie9-mobile-on-windows-phone-7-5/的查找頁面選項的javascript函數,該函數將通過點擊事件來訪問。我的實現在下面顯示了上面的鏈接中顯示的查找頁面搜索欄,但沒有發生任何其他事情,只有搜索欄顯示,無法使用。任何想法如何正確使用這個JavaScript函數通過我的點擊事件?提前致謝!在c#中使用javascript不工作

的Javascript

javascript:(
function() 
{ 
    function G() 
    { 
     var pf=doc.getElementById('pf'); 
     var qt=doc.getElementById('qt'); 

     if(null==pf) 
     { 
      pf=doc.createElement('div'); 
      pf.id='pf'; 
      var s=pf.style; 
      s.position='absolute'; 
      s.zIndex='99'; 
      s.top=(scT||scBT)+'px'; 
      s.left=(scL||scBL)+'px'; 
      s.width='100%'; 
      s.backgroundColor='#FFFF00'; 
      pf.appendChild(doc.createTextNode('Search: ')); 
      qt=doc.createElement('input'); 
      qt.id='qt'; 
      qt.type='text'; 
      pf.appendChild(qt); 
      var sb=doc.createElement('input'); 
      sb.type='button'; 
      sb.value='Find'; 
      sb.onclick=function() 
      { 
       P(qt.value) 
      }; 
      pf.appendChild(sb); 
      doc.body.appendChild(pf); 
     } 
     else 
     { 
      pf.style.display='inline'; 
      count=0; 
     } 
    } 
function P(s) 
    { 
     document.getElementById('pf').style.display='none'; 
     if(s==='') 
      return; 
     var n=srchNode(document.body,s.toUpperCase(),s.length); 
     alert("Found "+count+" occurrence"+(count==1?"":"s")+" of '"+s+"'."); 
     pf.parentNode.removeChild(pf); 
     return n; 
    } 
function srchNode(node,te,len) 
    { 
     var pos,skip,spannode,middlebit,endbit,middleclone; 
     skip=0; 
     if(node.nodeType==3) 
     { 
      pos=node.data.toUpperCase().indexOf(te); 
      if(pos>=0) 
      { 
       spannode=document.createElement("SPAN"); 
       spannode.style.backgroundColor="red"; 
       middlebit=node.splitText(pos); 
       endbit=middlebit.splitText(len); 
       middleclone=middlebit.cloneNode(true); 
       spannode.appendChild(middleclone); 
       middlebit.parentNode.replaceChild(spannode,middlebit); 
       ++count; 
       skip=1; 
      } 
     } 
     else 
     { 
      if(node.nodeType==1&&node.childNodes&&node.tagName.toUpperCase()!="SCRIPT"&&node.tagName.toUpperCase!="STYLE") 
     { 
      for(var child=0;child<node.childNodes.length;++child) 
      { 
       child=child+srchNode(node.childNodes[child],te,len); 
      } 
     } 
    } 
    return skip; 
} 
var count=0,scL=0,scT=0,scBL=0,scBT=0; 
var w=window,doc=document; 
if(typeof doc.body!='undefined'&&typeof doc.body.scrollLeft!='undefined') 
{ 
    scBL=doc.body.scrollLeft; 
    scBT=doc.body.scrollTop; 
} 
if(typeof doc.documentElement!='undefined'&&typeof doc.documentElement.scrollLeft!='undefined') 
{ 
    scL=doc.documentElement.scrollLeft; 
    scT=doc.documentElement.scrollTop; 
} 
G(); 
})() 

Click事件

public void FindOnPage() 
    { 
     var resource = Application.GetResourceStream(new Uri("Resources/FindOnPage.txt", UriKind.Relative)); 
     string text; 
     StreamReader sr = new StreamReader(resource.Stream); 

     while((text = sr.ReadToEnd()) != null) 
     { 
      TheWebBrowser.InvokeScript("eval", text); 
     }   
    } 

注意,JavaScript函數放置在文本文件中FindOnPage.txt。

+2

顯示您擁有的代碼。 – 2012-03-27 04:24:49

+0

@Robert Harvey我編輯了我的評論以顯示我的代碼。目前JavaScript功能正在通過點擊事件進行訪問,但只有頁面搜索欄上的查找顯示,但我無法使用它在原始文章鏈接中顯示的內容。如何解決這個問題的任何想法,以便頁面上的JavaScript查找功能正常工作? – Matthew 2012-04-05 05:37:40

回答

0

javascript函數正常工作。將FindOnPage方法更改爲以下內容

public void FindOnPage() 
    { 
     var resource = Application.GetResourceStream(new Uri("Resources/FindOnPage.txt", UriKind.Relative)); 
     string text; 
     StreamReader sr = new StreamReader(resource.Stream); 

     //while((text = sr.ReadToEnd()) != null) 
     if ((text = sr.ReadToEnd()) != null) 
     { 
      TheWebBrowser.InvokeScript("eval", text); 
     } 
    }