Click to See Complete Forum and Search --> : Open new window on Page Load


Pselus
10-13-2003, 02:05 PM
pretty simple right? Well I'm a VB.Net developer....we're not real programmers :p

anyway, all I need is a line of code to run through when the page refreshes...it can't be connected to a button click or anything like that...it just needs to run every time the page loads......
ideas?

Phil Karras
10-13-2003, 02:16 PM
<body onLoad='DoMe();'>

Where DoMe() is a JavaScript function in the <head> section of the HTML file that does what you want it to do.

Pselus
10-13-2003, 02:42 PM
thank you very much Phil

one more question though...this function you called DoMe() is being written in my ASP.Net server side code then placed in the client-side code. It happens on a button click. So what do I do the first time the page loads and that Javascript function doesn't exist? And with my .Net code I can't overwrite some javascript that's already there....and I can't change the <body> tag either....is there any way for me to stop the error that's bound to happen?

Phil Karras
10-13-2003, 04:32 PM
I'm going to assume all server-side scripting languages are pretty much the same, at least in things they can do if not in how they do it.

I know Perl & PHP, and VBasic so I assume I know something about ASP.NET.

The way I make sure a function exists is I include it in the base HTML file, or in a JS-LIB.js file that is included in the HTML file:


<head>
<script language="JavaScript" type="text/javascript" src='JS-LIB.JS'>
</script>
</head>


The way I normally do things is to have a base HTML file that is read in by my server-side script and added to or modified on-the-fly. I no longer create the entire HTML file inside my server-side scripts. In this way, if you only need to change a "COMMON" element in the base script (usually a *.txt file) it is an easy thing to do & doesn't require any mods to the server-side script.

One way or the other if you want a JS function to run onLoad then it MUST be either in the HTML file or an included JS script file.

Hope that helps.

Phil Karras
09-16-2009, 07:06 PM
I've actually had to do this same thing in a C# ASP.NET program so here it is:

First create the DoMe() function in your aspx file, at the top I believe it goes in <script type='text/JavaScript'> ... </script> tags between the first two main blocks.

Then at the bottom of the page you place another script tag set & in that set you set the onload = DoMe();

I can't remember off the top of my head if that is the correct syntax for the onload, probably not, but that's the idea.

Hope that helps, way late.
Phil Karras

Sabrina Gage
09-21-2009, 09:37 PM
Hi,
That is not so hard.

Show you demo:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script>
function openWindow()
{
window.open('http://www.google.com','a','height=300,width=600');
}
</script>
</head>
<body onload="openWindow();">

</body>
</html>

Phil Karras
09-22-2009, 07:53 PM
An ASPX file may not have a <body> tag, so you need to put another <script ...></script> tag set at the bottom of the page, there you can put an onLoad but it's not really needed, just put the line to run the function you want to run.

<script type"text/JavaScript">
RunMe();
</script>

or whatever the function name is you've used.

Phil Karras
09-23-2009, 01:50 PM
Pselus,

Here's exactly how I have a similar task running in my ASP.NET page, I use C#. Also, if you want to open a window then Sabrina Gage's window.open(...) does that so put that in your function or at the end of your page.

I used a two tier approach because I was using the main function not only from the onload of the page but also during another call. I also like putting the call at the bottom of the page because that helps to insure that the page is loaded by the time it tries to run the function. As it turned out on the aspx page there is no way to add a simple onload=, JavaScript? Or Jscript? Does not like it, it issues an error, even though it runs it just fine so I used two things to insure everything is fine before the function runs, 1. use window.onload = a function that calls the wanted function after a delay. The window.onload = a function had to be used in order to not cause the "Not implemented" error, the setTimeout was a little added insurance.

Another point, your page may re-load from time to time (when you do a post back, things like doing a database search, clicking a "View Details" button, all sorts of things) & that means that this "function" will run at those times as well. If that is NOT what you want then you'll need to write some code-behind to catch the times you do NOT want it to run your window function & tell it not to run at those times.

The way I do this is to set up a hidden var on the aspx page and in the code behind I change the value of the hidden field, then in the function that would open your window I would first check the value of that hidden field & do as directed.

First off, the method Sabrina used will work fine for the case where your aspx page has the HTML tags. So here are the assumptions I'm working under:

Assumption #1:
You want your function to run every time the page loads no matter what caused the page to load.

Assumption #2:
Your aspx page is using a MASTER page so there are NO HTML tags in the file. (Sabrina showed how to do this if your page does have HTML tags & that way should work just fine, at least it works fine in standard, non-aspx, HTML files.)

I used the following in a program of mine to test out the ideas when I needed them, here are the tests I did with comments about how they worked:
===============================================================================

<%@ Page Title="My Page" Language="C#" MasterPageFile="~/MyMasterPage.master" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<script type="text/JavaScript">

// Works BUT gives error: "Not implimented"
window.onload = TestFunction("directly #1");

function TestFunction(FrmHere) {
alert("In TestFunction() from: " + FrmHere);
}


// Works as wanted, no errors
window.onload = function() { setTimeout('TestFunction();', 50); }

</script>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">

<!-- The <body> start here -->

<asp:Panel ID="panelMain" runat="server">
.
.
.
<script type="text/javascript">

// Run this function after every page load

// Works BUT gives error: "Not implimented"
window.onload = TestFunction("Bottom of page, directly #2");

// Works correctly, no errors
window.onload = function() { setTimeout('TestFunction("Bottom of page, another function.");', 50); }

</script>


</asp:Panel>
</asp:Content>

===============================================================================

so, either placing the call to your function in the top script tags or script tags at the bottom of the page works HOWEVER as noted simply using window.onload does not work smoothly (in either location) since it causes an error while in the debugger saying that it was "Not implemented" which is rather strange since it ran the function AOK & then it gave the error.

If your aspx page has the HTML & body tags then use the method explained by Sabrina, if you're using a MASTER page so that your aspx page does not have HTML & body tags then you can use my method.

Also, if you want the window to open every time the MASTER page is used, again you can place the call in the body tag as Sabrina showed.

One last point, which probably does not need to be said, use the call to the window opener funtion either at the top or the bottom but not both.


Here's a super simple way to do it at the end of your aspx page, the above more complex methods work as well & have their uses but here's the simplest I've tested that works:

.
.
.

<script type="text/javascript">
window.open("http://cs.yrex.com");
</script>

</asp:Content>


=======================================================

OR the same thing can be put at the top of the page inside the first Content section:

====================================================================



<%@ Page Title="Test Page" Language="C#" MasterPageFile="~/MyMaster.master" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
window.open("http://cs.yrex.com");
</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">



====================================================================

That's about as simple as it can get, the script tags & one line!

Hope that helps.