Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Executing Javascript on Page_Load()

Posted on 2008-10-29
4
Medium Priority
?
398 Views
Last Modified: 2012-06-27
Hi all,
I have written a simple javascript function which opens a popup window to show search resluts. At the moment the Javascript is executed off the click of a button but i was wondering if it was possible to call the javascript function in the Page_Load() so the popup appears automatically when the page is loaded. Also i don't want to coming up on every PostBack so it would have to be when Page.IsPostBack = false.

Thanks in advance.
-M3mph15
0
Comment
Question by:M3mph15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 400 total points
ID: 22838037
please find sample code

also refer this link
http://bunkerhollow.com/blogs/matt/archive/2008/06/15/asp-net-execute-javascript-on-page-load.aspx
public Sub Page_Load(Sender As Object,e As EventArgs)     
       ' Form the script to be registered at client side.
'** add if ( !ispostback) if you only want to execute once
         Dim scriptString As String  = "<script language=JavaScript> function DoClick() {"
             scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}"
         scriptString += "function Page_Load(){ showMessage1.innerHTML="
         scriptString += "'<h4>RegisterStartupScript Example</h4>'}<"
         scriptString += "/"
         scriptString += "script>"
 
             If(Not Me.IsStartupScriptRegistered("Startup")) Then
        Me.RegisterStartupScript("Startup", scriptString)
             End If
    End Sub

Open in new window

0
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 1600 total points
ID: 22838210
<BODY onLoad="popupFunction();" onReload="javascript:return false;" >
0
 
LVL 6

Author Comment

by:M3mph15
ID: 22838225
Hi,
I tried what you have suggested. The Page Loads fine, no errors displayed but the window doesn't popup either. Here is my code:

Dim lhid As String = Request.QueryString("lhid")
lblLhId.Text = lhid
Dim scriptString As String = "<script type='text/javascript'> function showCon() {"
scriptString += "var obj_calwindow = window.open("
scriptString += "'ContactResults.aspx?type=con&lhid=' + " & lhid & " + '&contype=&condate=&loc=', 'Language', 'width=800,height='+'300'+"
scriptString += "',status=no,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'"
scriptString += ");"
scriptString += "}<"
scriptString += "/"
scriptString += "script>"
If (Not ClientScript.IsStartupScriptRegistered("Startup")) Then
ClientScript.RegisterStartupScript(GetType(Page), "Startup", scriptString)
End If
 Had to Modify Me.ClientScript because i kept getting a message saying in was obsolete.
What am i doing wrong?
-M3mph15
0
 
LVL 6

Author Comment

by:M3mph15
ID: 22838381
scrathcyboy your solution worked great. nice and Simple Thanks.  
BondinASP thanks for your suggestion. I didn't get time to play around with it to make it work. I think i was doing something wrong.
-M3mph15
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question