Need Help with Registering Client Scripts

Discussion in 'ASP .Net' started by Jonathan Wood, Jun 7, 2008.

  1. I'm trying to duplicate an HTML sample I have using my ASP.NET pages.

    The sample contains the following within the <head> tag:

    <script type="text/javascript" src="flashobject.js"></script>
    <script type="text/javascript">
    // <![CDATA[
    var args = new Object();
    var query = location.search.substring(1);
    // Get query string
    var pairs = query.split( "," );
    // Break at comma
    for ( var i = 0; i < pairs.length; i++ )
    {
    var pos = pairs.indexOf('=');
    if( pos == -1 )
    {
    continue; // Look for "name=value"
    }
    var argname = pairs.substring( 0, pos ); // If not found, skip
    var value = pairs.substring( pos + 1 ); // Extract the name
    args[argname] = unescape( value ); // Extract the value
    }
    // ]]>
    </script>

    To do this the ASP.NET way, I added the following code to my Page_Load
    handler:

    if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    "FlashObject", "flashobject.js");

    if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    "var args=new Object();" +
    "var query=location.search.substring(1);" +
    "var pairs=query.split(',');" +
    "for (var i=0;i < pairs.length;i++){" +
    "var pos=pairs.indexOf('=');" +
    "if (pos==-1) continue;" +
    "var argname=pairs.substring(0,pos);" +
    "var value=pairs.substring(pos + 1);" +
    "args[argname]=unescape(value);}", true);
    }

    I assume the second script is a start-up script since it is not a callable
    function. So this seems like it should work but it does not duplicate the
    functionality of the sample.

    Looking at the HTML produced, I see neither registered scripts are placed
    within the <head> tag. So instead of calling RegisterClientScriptInclude and
    RegisterStartupScript, I instead forced these scripts into the <head> tag
    and they WORKED!!

    Unfortunately, I need to put this in a control that really should register
    the scripts in an organized way.

    Is there any way to register a script such that it is inserted within the
    <head> tag? Or perhaps there's something else I'm missing?

    Thanks.

    Jonathan
     
    Jonathan Wood, Jun 7, 2008
    #1
    1. Advertising

  2. Jonathan Wood

    Munna Guest

    On Jun 7, 11:08 am, "Jonathan Wood" <> wrote:
    > I'm trying to duplicate an HTML sample I have using my ASP.NET pages.
    >
    > The sample contains the following within the <head> tag:
    >
    >    <script type="text/javascript" src="flashobject.js"></script>
    >    <script type="text/javascript">
    >       // <![CDATA[
    >       var args = new Object();
    >       var query = location.search.substring(1);
    >       // Get query string
    >       var pairs = query.split( "," );
    >       // Break at comma
    >       for ( var i = 0; i < pairs.length; i++ )
    >       {
    >          var pos = pairs.indexOf('=');
    >          if( pos == -1 )
    >          {
    >             continue; // Look for "name=value"
    >          }
    >          var argname = pairs.substring( 0, pos ); // If not found, skip
    >          var value = pairs.substring( pos + 1 ); // Extract the name
    >          args[argname] = unescape( value ); // Extract the value
    >       }
    >       // ]]>
    >    </script>
    >
    > To do this the ASP.NET way, I added the following code to my Page_Load
    > handler:
    >
    >   if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    >    Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    > "FlashObject", "flashobject.js");
    >
    >   if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    >    Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    >     "var args=new Object();" +
    >     "var query=location.search.substring(1);" +
    >     "var pairs=query.split(',');" +
    >     "for (var i=0;i < pairs.length;i++){" +
    >     "var pos=pairs.indexOf('=');" +
    >     "if (pos==-1) continue;" +
    >     "var argname=pairs.substring(0,pos);" +
    >     "var value=pairs.substring(pos + 1);" +
    >     "args[argname]=unescape(value);}", true);
    >  }
    >
    > I assume the second script is a start-up script since it is not a callable
    > function. So this seems like it should work but it does not duplicate the
    > functionality of the sample.
    >
    > Looking at the HTML produced, I see neither registered scripts are placed
    > within the <head> tag. So instead of calling RegisterClientScriptInclude and
    > RegisterStartupScript, I instead forced these scripts into the <head> tag
    > and they WORKED!!
    >
    > Unfortunately, I need to put this in a control that really should register
    > the scripts in an organized way.
    >
    > Is there any way to register a script such that it is inserted within the
    > <head> tag? Or perhaps there's something else I'm missing?
    >
    > Thanks.
    >
    > Jonathan


    Hi

    use this method to check wheiter script already added or not...

    Page.ClientScript.IsClientScriptBlockRegistered();

    and to register use

    Page.ClientScript.RegisterClientScriptBlock();

    Best of luck

    Munna
    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
     
    Munna, Jun 7, 2008
    #2
    1. Advertising

  3. > use this method to check wheiter script already added or not...
    >
    > Page.ClientScript.IsClientScriptBlockRegistered();
    >
    > and to register use
    >
    > Page.ClientScript.RegisterClientScriptBlock();


    Well, thanks. But not only does this not address the problem I've described,
    you're telling me to do exactly what the code I posted shows I was already
    doing.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Munna" <> wrote in message
    news:...
    On Jun 7, 11:08 am, "Jonathan Wood" <> wrote:
    > I'm trying to duplicate an HTML sample I have using my ASP.NET pages.
    >
    > The sample contains the following within the <head> tag:
    >
    > <script type="text/javascript" src="flashobject.js"></script>
    > <script type="text/javascript">
    > // <![CDATA[
    > var args = new Object();
    > var query = location.search.substring(1);
    > // Get query string
    > var pairs = query.split( "," );
    > // Break at comma
    > for ( var i = 0; i < pairs.length; i++ )
    > {
    > var pos = pairs.indexOf('=');
    > if( pos == -1 )
    > {
    > continue; // Look for "name=value"
    > }
    > var argname = pairs.substring( 0, pos ); // If not found, skip
    > var value = pairs.substring( pos + 1 ); // Extract the name
    > args[argname] = unescape( value ); // Extract the value
    > }
    > // ]]>
    > </script>
    >
    > To do this the ASP.NET way, I added the following code to my Page_Load
    > handler:
    >
    > if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    > Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    > "FlashObject", "flashobject.js");
    >
    > if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    > Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    > "var args=new Object();" +
    > "var query=location.search.substring(1);" +
    > "var pairs=query.split(',');" +
    > "for (var i=0;i < pairs.length;i++){" +
    > "var pos=pairs.indexOf('=');" +
    > "if (pos==-1) continue;" +
    > "var argname=pairs.substring(0,pos);" +
    > "var value=pairs.substring(pos + 1);" +
    > "args[argname]=unescape(value);}", true);
    > }
    >
    > I assume the second script is a start-up script since it is not a callable
    > function. So this seems like it should work but it does not duplicate the
    > functionality of the sample.
    >
    > Looking at the HTML produced, I see neither registered scripts are placed
    > within the <head> tag. So instead of calling RegisterClientScriptInclude
    > and
    > RegisterStartupScript, I instead forced these scripts into the <head> tag
    > and they WORKED!!
    >
    > Unfortunately, I need to put this in a control that really should register
    > the scripts in an organized way.
    >
    > Is there any way to register a script such that it is inserted within the
    > <head> tag? Or perhaps there's something else I'm missing?
    >
    > Thanks.
    >
    > Jonathan
     
    Jonathan Wood, Jun 7, 2008
    #3
  4. Jonathan Wood

    Munna Guest

    On Jun 7, 11:55 am, "Jonathan Wood" <> wrote:
    > > use this method to check wheiter script already added or not...

    >
    > > Page.ClientScript.IsClientScriptBlockRegistered();

    >
    > > and to register use

    >
    > > Page.ClientScript.RegisterClientScriptBlock();

    >
    > Well, thanks. But not only does this not address the problem I've described,
    > you're telling me to do exactly what the code I posted shows I was already
    > doing.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programminghttp://www.softcircuits.com
    >
    > "Munna" <> wrote in message
    >
    > news:...
    > On Jun 7, 11:08 am, "Jonathan Wood" <> wrote:
    >
    > > I'm trying to duplicate an HTML sample I have using my ASP.NET pages.

    >
    > > The sample contains the following within the <head> tag:

    >
    > > <script type="text/javascript" src="flashobject.js"></script>
    > > <script type="text/javascript">
    > > // <![CDATA[
    > > var args = new Object();
    > > var query = location.search.substring(1);
    > > // Get query string
    > > var pairs = query.split( "," );
    > > // Break at comma
    > > for ( var i = 0; i < pairs.length; i++ )
    > > {
    > > var pos = pairs.indexOf('=');
    > > if( pos == -1 )
    > > {
    > > continue; // Look for "name=value"
    > > }
    > > var argname = pairs.substring( 0, pos ); // If not found, skip
    > > var value = pairs.substring( pos + 1 ); // Extract the name
    > > args[argname] = unescape( value ); // Extract the value
    > > }
    > > // ]]>
    > > </script>

    >
    > > To do this the ASP.NET way, I added the following code to my Page_Load
    > > handler:

    >
    > > if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    > > Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    > > "FlashObject", "flashobject.js");

    >
    > > if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    > > Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    > > "var args=new Object();" +
    > > "var query=location.search.substring(1);" +
    > > "var pairs=query.split(',');" +
    > > "for (var i=0;i < pairs.length;i++){" +
    > > "var pos=pairs.indexOf('=');" +
    > > "if (pos==-1) continue;" +
    > > "var argname=pairs.substring(0,pos);" +
    > > "var value=pairs.substring(pos + 1);" +
    > > "args[argname]=unescape(value);}", true);
    > > }

    >
    > > I assume the second script is a start-up script since it is not a callable
    > > function. So this seems like it should work but it does not duplicate the
    > > functionality of the sample.

    >
    > > Looking at the HTML produced, I see neither registered scripts are placed
    > > within the <head> tag. So instead of calling RegisterClientScriptInclude
    > > and
    > > RegisterStartupScript, I instead forced these scripts into the <head> tag
    > > and they WORKED!!

    >
    > > Unfortunately, I need to put this in a control that really should register
    > > the scripts in an organized way.

    >
    > > Is there any way to register a script such that it is inserted within the
    > > <head> tag? Or perhaps there's something else I'm missing?

    >
    > > Thanks.

    >
    > > Jonathan


    Hi Jonathan

    "RegisterStartupScript" add scripts just before the end of "</body>"
    tag..
    and "RegisterClientScriptBlock" add script inside the form as far as i
    seen so far..
    well since you need the script in the head block here is a work around
    i found and worked for me...

    i added a literal control in size the head tag

    <head id="Myhead" runat="server">
    <title>Untitled Page</title>
    <asp:Literal ID="ScriptSource" runat="server"></asp:Literal>
    </head>

    and in page load event i did this

    ScriptSource.Text = "<script>window.alert('Worked okay');</script>";

    surely you need to do some coding to adjust what you want to insert in
    head...

    Best of luck

    Munna
    www.munna.shatkotha.com
    www.munna.shatkotha.com/blog
    www.shatkotha.com
     
    Munna, Jun 7, 2008
    #4
  5. I figured this out. Turns out that it is not necessary for these scripts to
    be within the <head> tag. I don't understand exactly how they are used but
    if I don't register the second script as a startup script and instead
    register it as a regular client script block, it appears to work just fine.
    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "Munna" <> wrote in message
    news:...
    > On Jun 7, 11:55 am, "Jonathan Wood" <> wrote:
    >> > use this method to check wheiter script already added or not...

    >>
    >> > Page.ClientScript.IsClientScriptBlockRegistered();

    >>
    >> > and to register use

    >>
    >> > Page.ClientScript.RegisterClientScriptBlock();

    >>
    >> Well, thanks. But not only does this not address the problem I've
    >> described,
    >> you're telling me to do exactly what the code I posted shows I was
    >> already
    >> doing.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programminghttp://www.softcircuits.com
    >>
    >> "Munna" <> wrote in message
    >>
    >> news:...
    >> On Jun 7, 11:08 am, "Jonathan Wood" <> wrote:
    >>
    >> > I'm trying to duplicate an HTML sample I have using my ASP.NET pages.

    >>
    >> > The sample contains the following within the <head> tag:

    >>
    >> > <script type="text/javascript" src="flashobject.js"></script>
    >> > <script type="text/javascript">
    >> > // <![CDATA[
    >> > var args = new Object();
    >> > var query = location.search.substring(1);
    >> > // Get query string
    >> > var pairs = query.split( "," );
    >> > // Break at comma
    >> > for ( var i = 0; i < pairs.length; i++ )
    >> > {
    >> > var pos = pairs.indexOf('=');
    >> > if( pos == -1 )
    >> > {
    >> > continue; // Look for "name=value"
    >> > }
    >> > var argname = pairs.substring( 0, pos ); // If not found, skip
    >> > var value = pairs.substring( pos + 1 ); // Extract the name
    >> > args[argname] = unescape( value ); // Extract the value
    >> > }
    >> > // ]]>
    >> > </script>

    >>
    >> > To do this the ASP.NET way, I added the following code to my Page_Load
    >> > handler:

    >>
    >> > if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    >> > Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    >> > "FlashObject", "flashobject.js");

    >>
    >> > if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    >> > Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    >> > "var args=new Object();" +
    >> > "var query=location.search.substring(1);" +
    >> > "var pairs=query.split(',');" +
    >> > "for (var i=0;i < pairs.length;i++){" +
    >> > "var pos=pairs.indexOf('=');" +
    >> > "if (pos==-1) continue;" +
    >> > "var argname=pairs.substring(0,pos);" +
    >> > "var value=pairs.substring(pos + 1);" +
    >> > "args[argname]=unescape(value);}", true);
    >> > }

    >>
    >> > I assume the second script is a start-up script since it is not a
    >> > callable
    >> > function. So this seems like it should work but it does not duplicate
    >> > the
    >> > functionality of the sample.

    >>
    >> > Looking at the HTML produced, I see neither registered scripts are
    >> > placed
    >> > within the <head> tag. So instead of calling
    >> > RegisterClientScriptInclude
    >> > and
    >> > RegisterStartupScript, I instead forced these scripts into the <head>
    >> > tag
    >> > and they WORKED!!

    >>
    >> > Unfortunately, I need to put this in a control that really should
    >> > register
    >> > the scripts in an organized way.

    >>
    >> > Is there any way to register a script such that it is inserted within
    >> > the
    >> > <head> tag? Or perhaps there's something else I'm missing?

    >>
    >> > Thanks.

    >>
    >> > Jonathan

    >
    > Hi Jonathan
    >
    > "RegisterStartupScript" add scripts just before the end of "</body>"
    > tag..
    > and "RegisterClientScriptBlock" add script inside the form as far as i
    > seen so far..
    > well since you need the script in the head block here is a work around
    > i found and worked for me...
    >
    > i added a literal control in size the head tag
    >
    > <head id="Myhead" runat="server">
    > <title>Untitled Page</title>
    > <asp:Literal ID="ScriptSource" runat="server"></asp:Literal>
    > </head>
    >
    > and in page load event i did this
    >
    > ScriptSource.Text = "<script>window.alert('Worked okay');</script>";
    >
    > surely you need to do some coding to adjust what you want to insert in
    > head...
    >
    > Best of luck
    >
    > Munna
    > www.munna.shatkotha.com
    > www.munna.shatkotha.com/blog
    > www.shatkotha.com
     
    Jonathan Wood, Jun 7, 2008
    #5
  6. Jonathan Wood

    bruce barker Guest

    your inline script (the second), just produces a global variables
    "args", "query" and "pairs". presumably they are used by another script.
    you need to find the references and be sure these script come first.
    if flashobject.js is the consumer, then you have the order backwards.

    -- bruce (sqlwork.com)

    Jonathan Wood wrote:
    > I'm trying to duplicate an HTML sample I have using my ASP.NET pages.
    >
    > The sample contains the following within the <head> tag:
    >
    > <script type="text/javascript" src="flashobject.js"></script>
    > <script type="text/javascript">
    > // <![CDATA[
    > var args = new Object();
    > var query = location.search.substring(1);
    > // Get query string
    > var pairs = query.split( "," );
    > // Break at comma
    > for ( var i = 0; i < pairs.length; i++ )
    > {
    > var pos = pairs.indexOf('=');
    > if( pos == -1 )
    > {
    > continue; // Look for "name=value"
    > }
    > var argname = pairs.substring( 0, pos ); // If not found, skip
    > var value = pairs.substring( pos + 1 ); // Extract the name
    > args[argname] = unescape( value ); // Extract the value
    > }
    > // ]]>
    > </script>
    >
    > To do this the ASP.NET way, I added the following code to my Page_Load
    > handler:
    >
    > if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    > Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    > "FlashObject", "flashobject.js");
    >
    > if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    > Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    > "var args=new Object();" +
    > "var query=location.search.substring(1);" +
    > "var pairs=query.split(',');" +
    > "for (var i=0;i < pairs.length;i++){" +
    > "var pos=pairs.indexOf('=');" +
    > "if (pos==-1) continue;" +
    > "var argname=pairs.substring(0,pos);" +
    > "var value=pairs.substring(pos + 1);" +
    > "args[argname]=unescape(value);}", true);
    > }
    >
    > I assume the second script is a start-up script since it is not a
    > callable function. So this seems like it should work but it does not
    > duplicate the functionality of the sample.
    >
    > Looking at the HTML produced, I see neither registered scripts are
    > placed within the <head> tag. So instead of calling
    > RegisterClientScriptInclude and RegisterStartupScript, I instead forced
    > these scripts into the <head> tag and they WORKED!!
    >
    > Unfortunately, I need to put this in a control that really should
    > register the scripts in an organized way.
    >
    > Is there any way to register a script such that it is inserted within
    > the <head> tag? Or perhaps there's something else I'm missing?
    >
    > Thanks.
    >
    > Jonathan
    >
     
    bruce barker, Jun 8, 2008
    #6
  7. Yeah, that seems to have been the issue. By inserting the script not as a
    startup script (which ASP.NET seems to put at the end) it works. I'm still
    not clear on how it runs if it's neither a function or a startup script. But
    that seems to be the case. Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "bruce barker" <> wrote in message
    news:...
    > your inline script (the second), just produces a global variables "args",
    > "query" and "pairs". presumably they are used by another script. you need
    > to find the references and be sure these script come first. if
    > flashobject.js is the consumer, then you have the order backwards.
    >
    > -- bruce (sqlwork.com)
    >
    > Jonathan Wood wrote:
    >> I'm trying to duplicate an HTML sample I have using my ASP.NET pages.
    >>
    >> The sample contains the following within the <head> tag:
    >>
    >> <script type="text/javascript" src="flashobject.js"></script>
    >> <script type="text/javascript">
    >> // <![CDATA[
    >> var args = new Object();
    >> var query = location.search.substring(1);
    >> // Get query string
    >> var pairs = query.split( "," );
    >> // Break at comma
    >> for ( var i = 0; i < pairs.length; i++ )
    >> {
    >> var pos = pairs.indexOf('=');
    >> if( pos == -1 )
    >> {
    >> continue; // Look for "name=value"
    >> }
    >> var argname = pairs.substring( 0, pos ); // If not found, skip
    >> var value = pairs.substring( pos + 1 ); // Extract the name
    >> args[argname] = unescape( value ); // Extract the value
    >> }
    >> // ]]>
    >> </script>
    >>
    >> To do this the ASP.NET way, I added the following code to my Page_Load
    >> handler:
    >>
    >> if (!Page.ClientScript.IsClientScriptBlockRegistered("FlashObject"))
    >> Page.ClientScript.RegisterClientScriptInclude(typeof(Page),
    >> "FlashObject", "flashobject.js");
    >>
    >> if (!Page.ClientScript.IsStartupScriptRegistered("PrepVideo"))
    >> Page.ClientScript.RegisterStartupScript(typeof(Page), "PrepVideo",
    >> "var args=new Object();" +
    >> "var query=location.search.substring(1);" +
    >> "var pairs=query.split(',');" +
    >> "for (var i=0;i < pairs.length;i++){" +
    >> "var pos=pairs.indexOf('=');" +
    >> "if (pos==-1) continue;" +
    >> "var argname=pairs.substring(0,pos);" +
    >> "var value=pairs.substring(pos + 1);" +
    >> "args[argname]=unescape(value);}", true);
    >> }
    >>
    >> I assume the second script is a start-up script since it is not a
    >> callable function. So this seems like it should work but it does not
    >> duplicate the functionality of the sample.
    >>
    >> Looking at the HTML produced, I see neither registered scripts are placed
    >> within the <head> tag. So instead of calling RegisterClientScriptInclude
    >> and RegisterStartupScript, I instead forced these scripts into the <head>
    >> tag and they WORKED!!
    >>
    >> Unfortunately, I need to put this in a control that really should
    >> register the scripts in an organized way.
    >>
    >> Is there any way to register a script such that it is inserted within the
    >> <head> tag? Or perhaps there's something else I'm missing?
    >>
    >> Thanks.
    >>
    >> Jonathan
    >>
     
    Jonathan Wood, Jun 8, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Danielsson
    Replies:
    8
    Views:
    669
    Mike Meyer
    Jul 22, 2005
  2. Jp Calderone
    Replies:
    0
    Views:
    483
    Jp Calderone
    Jul 21, 2005
  3. Juan T. Llibre
    Replies:
    2
    Views:
    4,071
    =?Utf-8?B?UmljaA==?=
    Dec 16, 2006
  4. davidj411
    Replies:
    0
    Views:
    583
    davidj411
    Jun 27, 2008
  5. Replies:
    13
    Views:
    581
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page