Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to execute this Javascript method.This sets the window title of an Internet Explorer tab that doesn't have a title:

JavaScript
<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>


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

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
               );
           }
       }
Posted
Comments
There is a value you are checking in if condition that is SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS.
You have not defined it anywhere. So, it would not work. See what errors you see in Developer Tool Console Window.

1 solution

C#
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';
}
%>]]>


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
                );
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900