Avoid reloading head scripts when page load via Ajax - asp.net

I have a main page via a div. Many page are load into div via "load" function in jQuery.
When page loaded all script in head of page is load.
Is there a way to prevent scripts from being reloaded when the script is already loaded?

(Disclaimer: this architecture is wrong and should not be used. My response is only to address the OP's specific problem)
Try wrapping the head area of your pages in an if statement, which checks whether the request is ajax or not:
<%if(!Request.IsAjaxRequest){%>
<head>
...
</head>
<%}%>
Update
In response to OP's comment - Add a querystring parameter to every ajax request that happens after the first one. Use some JavaScript for that. Then change the if statement to check for that parameter:
<%if(String.IsNullOrEmpty(Request.QueryString["secondTime"]){%>
<head>
...
</head>
<%}%>

Related

Need parent name of Iframe

I've been searching for a couple hours and haven't found a solution. Hopefully you can help.
I want to get the name & path of the parent page within an Iframe. The reason for this is to prevent the page within the Iframe to be displayed outside it's intended location. If I can get the session variables of the parent page, that would be even better as I could verify the user.
The Iframe will be written in ASP.NET, while the parent page is PHP.
I have found Javascript code that might work, but I'm very unfamiliar with javascript and how to pass the return value to the code behind in ASP.NET. If your solution involves javascript, I will need the whole thing from calling it in the form load to getting back the value.
I have tried these functions, but they don't show what I need.
Request.Url.AbsoluteUri.ToString
Request.ApplicationPath.ToString
Request.UrlReferrer.AbsolutePath.ToString
Thanks
I understand that you looking for a code behind solution and redirect, but the code behind can not recognize if the page is calling from some other.
You may try the Request.ServerVariables["HTTP_REFERER"] but this can make mistake, and can even been disable by some users, or changed.
One simple working solution with javascript is to check on the iframe it self if its inside your master page or not. So this code go to the page that lives inside the iframe, and if see that is alone is redirect the user back to the master page.
<script type="text/javascript" language="javascript">
// this line is check if its inside an iframe.
// if top==self then is not in iframe
if(top == self)
{
alert("This page can not be viewed alone, please press ok to load the master page");
top.location.replace("MasterPage.aspx");
}
</script>

What is the difference between ScriptManager.RegisterStartupScript() and ScriptManager.RegisterClientScriptBlock(), as both does the same thing?

Hi
I am just wondering why some people suggest RegisterStartupScript() to call client side js while some suggests RegisterClientScriptBlock().
Please make me clear whats the difference between the two as they doing the same operations, for using js statement calls and which one is preferable if I only use js statements like alert, return confirm from codebehind.
The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet.
The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element. The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.
Normally you would use RegisterClientScriptBlock if you want to register js-functions and use RegisterStartupScript if you want to call these functions or want to access controls on your page.
For further informations have a look at MSDN.

jQuery undefined in content page when using master pages which has jQuery reference

I have a nested master page. A parent master page, a child master page and a content page whose master page is the child master page. I have a reference to jQuery in the parent master page in the head section.<script type="text/javascript" src='<%#ResolveUrl("~/includes/jquery-1.4.2.min.js") %>' ></script> & Page.Header.DataBind(); in the OnLoad event.
I am using jQuery in all the pages including the master pages. However I am getting "Error: $().ready is not a function" in the content page. If I include jQuery reference in the content page it works.
Question: If the reference to jQuery is in the master page head section, why aren't the content pages able to use jQuery? When I do view source, the script tag with jQuery is there and it works.
The master pages and content page are merged during rendering and sent to the browser as a single html page so I am not sure when master pages are used, jQuery references break.
UPDATE:
When I changed '$.ready(function()' to 'jQuery(document).ready(function($)' it worked! I am not loading any other javascript libraries and I am not using MS Ajax.
First, and I didn't notice this before, but is your original call to $.ready(function() {}), which didn't work, but jQuery(document).ready(function() {}) did work? Does your call work if you use $(document).ready(function () {} )? Just wanted to make sure it's not a typo. The jQuery docs say the $.ready(function() {}) is valid, but it's not recommended.
OK, assuming it's not a typo, it definitely sounds like you have a conflict with the '$' variable. If you're using third-party controls or ASP.NET AJAX, you may run into conflicts (even though you may not be including the JS file explicitly).
If you could post what gets output by the browser after your page loads, that would help.
Also, if you run Fiddler (or some other traffic request tool), you can see if any JS references are being downloaded. Look not only for .JS files, but also .AXD files (some third-party tools name these files ScriptResource.axd or WebResource.axd, and they may redefine the '$' variable).
You may want to check out this link on the jQuery API page about the noConflict function. This helps when you have a conflict with the $ variable.
Without seeing the output, it's tough to diagnose. But hopefully this helps.
i also have the same issue, it sounds like a jQuery conflict
i fixed my issue :
<script type="text/javascript" >
$(document).ready(function () {
$.noConflict();
});
</script>
in the <head> section

Why Are There Multiple GET Requests Shown in Firebug?

So I'm at the stage in web programming where I'm past the "Look, Ma, I can put data in a grid and it shows up on the page." I'm now at the, wow, this site is not as snappy as I want it to be. So, I enabled the "Net" tab in Firebug, closed my eyes, crossed my fingers, and went spelunking.
The first thing I noticed is that all of my .aspx pages are being "GET"ed at least three times. Is this normal? If not, what is "normal"? What affects the "GET"ing of the .aspx pages? I'm assuming that includes the time it takes to hit the database and render all the controls on the page. Is that true?
Perhaps what would really benefit me is a place I can look to get some "best practices" for these kinds of speed related issues.
Things to consider:
Using IIS 6.0 via HTTPS
We're using Masterpages
We're using Telerik controls
A RadMenu
A RadScriptManager
I'm certainly more of a thick client guy than a web guy
EDIT
Answers to questions below:
The response code is 200
EDIT
Screen shot added:
FirebugScreenshot http://img187.imageshack.us/img187/5873/firebughelp.jpg
EDIT Added Additional Screenshot to include Request Headers
EDIT
Added links
page source
My Master file as txt
My WebConfig (without connection strings, obviously)
EDIT: Here is the source of your two extra page loads:
<script type="text/javascript"
src='<%# ResolveUrl("~/Common/jQuery/jquery-1.3.2.min.js") %>'>
</script>
<script type="text/javascript"
src='<%# ResolveUrl("~/Common/jQuery/jquery-ui-1.7.1.custom.min.js") %>'>
</script>
As you can see in the rendered version the src attribute is empty, causing it to load the page two extra times.
<script type="text/javascript" src=''></script>
<script type="text/javascript" src=''></script>
You can probably fix this by using the runat server tag and having it resolve the urls automatically.
<script type="text/javascript"
src="~/Common/jQuery/jquery-1.3.2.min.js"
runat="server"
ID="jQuery"> </script>
<script type="text/javascript"
src="~/Common/jQuery/jquery-ui-1.7.1.custom.min.js"
runat="server"
ID="jQueryUI"> </script>
(or change <%# %> to <%= %> -- since you need to have the version that outputs a string instead of the binding syntax).
Original answer removed since it was not related to the actual problem.
It's unlikely that those are AJAX requests, as the response length is the same on each request.
I'm also ruling out the bug with empty src attribute of img elements, as this only causes one reload of the page, not two.
There is a know bug with Telerik RadEditor that might cause such condition, but you don't mention it in the list of used controls. Here are more details about it:
http://www.telerik.com/community/forums/aspnet-ajax/editor/radeditor-forces-page-load-twice.aspx
You might also want to comment out the Telerik controls on the page to see if that helps.
The browser should normally hit the server just once, and all the time it takes to query the database and whatnot should be confined within that request. If you're playing around with ajax controls, they're likely to query the server more times for new data. You can use firebug to inspect the requests and responses, and see what they contain.
A common cause for the aspx being requested several times is having IMG tags rendered without any SRC attribute. This will default to querying the same page for the image source. If this is the case for you, then you could check the request headers in firebug, to see if it expects an image.
You could also go to the console and type document.images to get a list of all the images. The ones that aren't visible on the page will be shown slightly faded. Inspect those for blank SRC's.
This normally happen if you are in the Development Environment and "Enable browser link" in VS 2015/2013 .
To avoid the Multiple GET Requests Shown in Firebug, uncheck "Enable browser link" from the tool bar.
If you view the page source, you find script tag added to the page when you check "Enable Browser Link". This causes Get/Post Actions to the iis server. For more details: http://www.asp.net/visual-studio/overview/2013/using-browser-link
The browser makes a GET request for each resource included in the Page, including js files, images, css files...
It could be lots of things - the important part of the request is what it's GETing.
Generally you're going to see more than one request for an ASPX page as it loads the javascript libraries to perform validation and postbacks. Controls can also have javascript embedded as resources, which in turn create other GET requests, usually for WebResource.axd and ScriptResource.axd.
If you have security enabled, that could be the challenge and response requests - first a 401 then a 200. What are the response codes that you are getting?

iframes and redirection

I have a webpage (say www.example.com/a.asp) .. I am having an iframe in this page which loads a page from a different website (www.example.net/b.asp)... This b.asp asks few questions to the user and the results are posted to c.asp in my website (www.example.com/c.asp). This page (www.example.com/c.asp) gets loaded in the iframe. Is there any way so that I can reload the entire webpage and redirect to another wbpage when I get the response from www.example.net. Sorry if this question is confusing, Any queries please let me know.
As mentioned above, you can use the "_top" in the target for the form, or you could use some JavaScript on the result page to check and see if its the "top" page. All things being equal, you're better off avoiding the JavaScript as it won't work if the client has scripting disabled.
If i understand correctly i think you might be able to set the target for the post to c.asp to be "_top". This should reload the entire page getting you out of the iframe. Your c.asp page would have to handle the redirection you require.
Alternatively, the redirection that the c.asp page needs to perform may be able to target the "_top" itself, in case you can't change the page which posts the data.
Mhm, not sure if I got you right. Do you really mean when you got the response from abc.com rather than your own web site?
The form tag in HTML seems to have a target-attribute for loading the response to a specified (e.g. _top) target after submitting data:
http://de.selfhtml.org/html/referenz/attribute.htm#form (German)
Not sure if I really answered your question. Did I?
Cheers
Matthias
If you have no control over the resource loaded into the frame, you can't redirect using HTTP, so you are stick with JavaScript.
Given the same origin policy, I think the best you can do is to determine when a new document is loaded into the iframe.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<title>test of iframe</title>
<iframe width="500" height="400" src="http://example.net/">
<p>See The Example Site</p>
</iframe>
<script type="text/javascript">
window.onload = function () {
document.getElementsByTagName('iframe')[0].onload = function () {
window.alert('Document loaded in frame');
}
}
</script>
You can then set window.location (possibly after counting the number of times the event fires)

Resources