last day (15 days later) » 

9:56 AM
0
Q: Get the querystring parameter in the page loaded using XMLHTTPRequest

jitendragargI am trying to load a page inside a div using below code. $("#ReportDiv").load("../pages/Report.html", function (response, status, xhr) { if (status == "error") { $("#ReportDiv").html("Unable to load the page"); } }); It is simple load function which works great for normal ...

 
@mplungjan Because I am loading this page inside a DIV. So, window.location will still give me url of the calling page, not the one opened inside the Div.
Dude, XMLHTTPRequest does not change "window.location". $("#ReportDiv").load("../pages/Report.html?userid=1", won't change the window.location value to "../pages/Report.html?userid=1`. So, please don't bother. I would rather expect help from someone who reads question for more than 1 second.
 
@jitendragarg there is no reason to rant at Rory. As you can see he is one of the top providers of answers at SO. If you cannot engage in a discussion, the SO is not for you
 
@mplungjan Took less than 10 seconds for him to mark the question as duplicate. I don't care how much time he spends here. There is no way, anyone can understand question and then find the duplicate answer in less than 10 seconds. I can't have a discussion with anyone who spends less than 10 seconds on a question and then marks it as duplicate without understanding the issue first. If that makes SO not for me, then I guess we can ask the moderator team about the same.
@RoryMcCrossan Yes, I read the comment. XMLHTTPRequest does not go to server. Also, less than 10 seconds to mark a question duplicate. Tell me again, why shall we be talking right now? You don't have to answer every question, mate.
 
I could have marked it too in less than 10 seconds. It is a very obvious question. If the Report.html is a php in disguise it can access the query parameters via the $_GET variables. If it is NOT, then the question is an X/Y problem. You have no need of the query parameters in Report.html, you can just save the parameters and then have the page that is loading Report.html do whatever is needed with those parms
 
Okay, that might work. I tried session storage btw. That doesn't work because the second page can't find the session details. Also, how is this duplicate of "use window.location". I did use window.location. I mentioned that in the question itself. It doesn't work with XMLHttpRequest, which people will know if they spend few seconds to understand the question. Don't you think?
 
9:56 AM
What second page? Report.html is not a second page. It is just some html that is rendered in the page when retrieved. There is no action possible in Report.html unless as I said it is actually a server generated page in which case the server generating code can access the GET array
 
Hey.
Thought we should move to chat.
 
Sure
What is Report.html?
 
Anyway, let me try to explain the flow.
 
Please do
 
I have a page called reportselection.html. This is part of an application where login works and I have user details in the session. This reportselection.html page is loading "Report1.html" or "Report2.html" using XHR.
I need to get the user details from reportselection page (login info entered and saved in session), in Report1.html.
 
9:59 AM
How. What is Report1.html? Just HTML or HTML with JS or a server processed page?
 
This information is used to call the api. And yes, api works fine if there is no parameters. Report1.html page also allows other processing like creating a table etc.
All of these pages are just HTML and JS. Server side calls are all API based using ajax.
 
Still not clear. If the API does not allow parameters how do you want to pass parameters?
 
I have multiple api. I tried with one that needs no parameter. It works fine. The one I need to use requires parameters.
 
So what is the problem?
 
If I use hardcoded parameters, say hardcoded userid, then everything works fine.
 
10:01 AM
It is very unclear
If the API allows parameters, send them if not, you need to insert stuff after the HTML is returned
before it is rendered
 
See the image.
 
Ok
 
From first page, when I select a report, I am using XHR to load the report page (second image).
 
Ok
 
In this second image, some api need userid of the logged in user.
 
10:04 AM
But the HTML is currently rendered in the very first page, no?
 
First page has that information in session storage. So, I am passing that value using querystring to second page.
Sorry yes.
 
and you can call the second API with ?userid=xxx
 
Let's call that first section and second section.
 
second page is an actual second page
section or page (URL)
 
yes, that is an actual html page that opens inside a div in first page.
 
10:05 AM
page1.html and page2.html or page1.html plus a load
Then it is NOT a page
"opens inside a div" is not a new page
 
page1.html. select any report. that opens page2 or page3.html using xhr inside a div.
 
So page1.html has all the jQuery that can ajax to api1 and api 2
and page2 and page3 are not actually pages but live inside div2 and div3
 
No, that is part of page2 and page3 and so on.
 
in page1
 
Since page1 does not know which page to open unless user selects one, I can't write the ajax calls in page1
 
10:08 AM
You lost me again. .load will load data, if the data is static html it will do no processing. If the data comes from an api, you can send parameters in the query string or you can use $.post
 
so, when page1 loads page2, it loads page2.js as well.
 
That is the missing info
You need to EXECUTE page2.js
That is another kind of processing
page2.js does not execute in a load
 
yes, it is executing fine. it is just unable to get the session data or querystring.
yes, that's correct. page2.js will execute only when page1 makes a call for page2.html.
 
Hmm. That is not how this should run. If you are saying that <script src="page2.js"></script> in page2.html loaded using .load is executing fine, then you need to not do it that way
 
Idea is that I have a main selection page which allows user to select specific report. Then that specific report page will call its own api etc to display or download the report.
 
10:11 AM
1
Q: When are JS scripts executed, if they are loaded with jQuery .html() function?

jacek.ciachI'm trying to figure out WHEN JavaScript is executed when one loads a HTML + JS content with .ajax() jQuery function and inserts the content with .html(content). I've read about domManip, read curious about jquery .load(), Load script tag from the file html, jQuery .load() / .ajax() not execut...

 
page2.js executes fine. It does everything as expected.
//Sets the report selection values and open the report filter tab
function OpenReportFilter(reportName) {
//to remove space
var Page = reportName.replace(/\s/g, '');

$("#ReportDiv").load("../../pages/Report" + Page + ".html?userid=5", function (response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error getting details ! ";
$("#ReportDiv").html(msg + xhr.status + " " + xhr.statusText);
$("#ReportDiv").html("Unable to load the page");
}
});
}
This is the code in reportselection.html page (page1).
 
So it seems you want the JS in page2 to access the parameters from the session. the JS should run in the scope of the PARENT page and read whatever JS has set in that page
 
In page 2,
//Initialize page controls
$(document).ready(function () {
alert(window.location);
This alert shows up.
But window.location shows page1.html as the path.
It is in scope, considering the page was loaded as XHR. Right?
 
So
var userid=5
in parent page

In page 2,
//Initialize page controls
$(document).ready(function () {
alert(userid);

should alert userid set in page1
 
Yes.
 
10:14 AM
it should
try it
make userid a global var and it should show
 
It is not a var though. It is part of the query string
$("#ReportDiv").load("../../pages/Report" + Page + ".html?userid=5
I haven't tried global var. Let me try. Give me 2 minutes.
 
userid = new URLSearchParams(location.search).get("userid")
will get the userid from page1's location
 
The second option works.
I don't know why global variable returns null always.
BTW, thanks for your help. Finally figured out how to read the url.
 
Are they truly global? They need to be outside $(document).ready
Anyway, do not be so angry next time. Your question can be closed, you can explain why it should not be and then it can be opened again. I will drop out of this convo npw
 

last day (15 days later) »