Need Help with Registering Client Scripts

J

Jonathan Wood

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
 
M

Munna

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
 
J

Jonathan Wood

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

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
 
M

Munna

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

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


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?

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
 
J

Jonathan Wood

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 said:
use this method to check wheiter script already added or not...

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


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?

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
 
B

bruce barker

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)
 
J

Jonathan Wood

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 said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top