如何在C#中的Javascript代码下方执行? [英] How can I execute below Javascript code in C#?

查看:385
本文介绍了如何在C#中的Javascript代码下方执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行此Javascript方法.这将设置没有标题的Internet Explorer选项卡的窗口标题:

I need to execute this Javascript method.This sets the window title of an Internet Explorer tab that doesn''t have a title:

<script>
if(window.name == ''){
if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){
 window.name = 'myWinName';
} else {
 //we have a new Tab/Window (or something funky)
 alert('What?! One window not cool enough for ya?\n' +
   'Calling the InterWeb Police!');
}
} else if(window.name == 'myWinName'){
document.title = 'All is well... we think';
}
</script>



我做到了,如下所示.但是当我从C#打印它时,它没有设置主窗口标题:



I did it like below. But it doesn''t set the main window title when I print it from C#:

SHDocVw.ShellWindows shellWindows1 = new SHDocVw.ShellWindows();
       foreach (SHDocVw.InternetExplorer ieInst in shellWindows1)
   {
       String str = "<script>if(window.name == ''){if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){window.name = 'myWinName';} else {alert('What?! One window not cool enough for ya?\n' +'Calling the InterWeb Police!');} } else if(window.name == 'myWinName'){document.title = 'All is well... we think';}</script>";
       Page x=new Page();
       Program p = new Program();
       x.ClientScript.RegisterStartupScript(p.GetType(), "Script", str, false);
       System.Console.WriteLine(ieInst.LocationURL+"\t");

   }
       foreach (Process theprocess in Process.GetProcesses("."))
       {
           if (theprocess.ProcessName == "iexplore")
           {
               Console.WriteLine("Process: {0}\tID: {1}\tWindow name: {2}",
                   theprocess.ProcessName, theprocess.Id, theprocess.MainWindowTitle + "\t" + theprocess.StartTime
               );
           }
       }

推荐答案

simply make use of <% %> tag
<![CDATA[<% 
if(window.name == ''){
if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){
 window.name = 'myWinName';
} else {
 //we have a new Tab/Window (or something funky)
 alert('What?! One window not cool enough for ya?\n' +
   'Calling the InterWeb Police!');
}
} else if(window.name == 'myWinName'){
document.title = 'All is well... we think';
}
%>]]>





SHDocVw.ShellWindows shellWindows1 = new SHDocVw.ShellWindows();
        foreach (SHDocVw.InternetExplorer ieInst in shellWindows1)
    {
        String str = "<script>if(window.name == ''){if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){window.name = 'myWinName';} else {alert('What?! One window not cool enough for ya?\n' +'Calling the InterWeb Police!');} } else if(window.name == 'myWinName'){document.title = 'All is well... we think';}</script>";
        Page x=new Page();
        Program p = new Program();
        x.ClientScript.RegisterStartupScript(p.GetType(), "Script", str, false);
        System.Console.WriteLine(ieInst.LocationURL+"\t");

    }
        foreach (Process theprocess in Process.GetProcesses("."))
        {
            if (theprocess.ProcessName == "iexplore")
            {
                Console.WriteLine("Process: {0}\tID: {1}\tWindow name: {2}",
                    theprocess.ProcessName, theprocess.Id, theprocess.MainWindowTitle + "\t" + theprocess.StartTime
                );
            }
        }


这篇关于如何在C#中的Javascript代码下方执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆