Forum Moderators: open

Message Too Old, No Replies

Location of a page to stay the same when calling a new url

How to do it?

         

tata668

4:25 am on Jan 15, 2010 (gmt 0)

10+ Year Member



The way I know to prevent the location of a page to change, when loading a new url, is to return an "attachment" instead of a new html page.

For example, using php, clicking a link with a "test.php" url may results in this php output:

==================


header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=test.txt");
echo "the content";
exit();

==================

This would results in a "save file as" dialog box to appear and the page would stay where it is: no new page would be loaded.

I wonder if there is a way to output something, some headers, that would prevent the location of the page to change as well, but without poping any dialog box? Some kind of "request cancellation" we might say.

Is there a way, for example, to return some kind of empty "text/javascript" content, which would be "interpreted" without the location of the page to change?

Any idea?

Thanks in advance!

Seb7

3:01 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



Did you try?

header("Content-Type: text/html");
header("Content-disposition: attachment; filename=test.htm");
echo "the content";
exit();

Your other options are either using a frame, an iframe or ajax.

tata668

6:20 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



It pops a dialog box asking if I want to open "test.htm"... So not what I'm looking for.

I know I can use ajax to make the request without the location of the page to change, but my question is really about clicking a link targetting the current window itself.

Thanks for the help anyway!

Fotiman

7:02 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The answer is no. That would pose a huge security risk, as you would essentially be viewing a page that was different from the URL.

tata668

7:41 pm on Jan 15, 2010 (gmt 0)

10+ Year Member




That would pose a huge security risk, as you would essentially be viewing a page that was different from the URL.

Not necessarily. When a "save as" dialog box pops, for example, there is no "other page viewed".

If there was a way to send some kind of "cancel request" headers, the url would still reflects the page loaded, since the url of the clicked link wouldn't even be shown in the url bar. And anyway, we all know it's possible to do anything to the page without chaging its url, if we use ajax.

Some ideas (didn't try them):

- output attachment headers but close the dialog box immediatly using javascript (is it possible?)

- output an invalid attachment that wouldn't even triggers the "save as" dialog box

- output some css or js content in a way the browser would interprets them instead of trying to open/save them.

tata668

7:58 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



Another idea that doesn't work. A redirect to a "javascript:" url that does nothing:

==============
header("Location: javascript:(function(){})()");
exit();
==============

Fotiman

8:48 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not necessarily. When a "save as" dialog box pops, for example, there is no "other page viewed".

That is because the browser has received an HTTP response with a specific mime type, and determines how to process that response. For some mime types, that means open a 3rd party program, for some it means provide a dialog to allow the person to save the content, and for some it means show the contents in the browser window (at which point, the browser will update the URL location to reflect it's current location). You can't act on the headers until the browser has finished receiving the response. In other words, the processing will be something like this:

1. user clicks link
2. browser sends HTTP request to server
3. server sends an HTTP response
4. browser interprets response and does appropriate action based on the HTTP headers and content returned (for example, show a web page, show a 404 error, provide a "save" dialog, pass the content to an appropriate handler for that mime type, etc.

Are there any HTTP headers that would cause the content to be displayed without changing the URL? Again, I don't believe so, as that would be a security risk (it would allow real sites to be spoofed). Your only option would be to use AJAX to send a request to the server, and then replace the contents of the current page with the results.

tata668

9:10 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



Fotiman,

I don't want "the content to be displayed without changing the URL". I just want the HTTP request to reach the server without changing the URL. No content to show or to display.

I know everything you said! I'm looking for some kind of secret headers that would cause the browser to do nothing at all! To discard the HTTP response or to react to it, but invisibly.

By the way, I'm only asking this question by technical curiosity! If I needed to make an invisible request to the server, in real life, I would simply use any ajax method.

Demaestro

9:33 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm looking for some kind of secret headers that would cause the browser to do nothing at all!

Then why return anything? Like this

exit();

I may be missing something here but if you want to return something but have the browser do nothing wouldn't that be the same as not returning a response at all? I mean save yourself a step, don't return anything and the browser will happily do nothing.

tata668

9:43 pm on Jan 15, 2010 (gmt 0)

10+ Year Member



Then why return anything? Like this

exit();

That would makes the page to change and the new url to appears in the url bar of the browser! But the page would be empty.

I don't want the url of the clicked link to appear in the url bar! And I don't want the current page to change!

Fotiman

9:46 pm on Jan 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, ok. So you're not actually expecting any data back, you just want some means of transporting data to the server via a link which sends an HTTP request (without changing the page). I don't think there is any way to do that... I believe that if you don't return an HTTP response to the browser, then it will appear as though it's stuck waiting for a response. And if you do return an HTTP response, then the browser is going to take some action. To my knowledge, because HTTP is a request/response protocol, you have to return something, and I don't know of any headers that will cause the behavior you're looking for.

tata668

10:02 pm on Jan 15, 2010 (gmt 0)

10+ Year Member




and I don't know of any headers that will cause the behavior you're looking for.

I'll try to find some!

Demaestro

4:39 pm on Jan 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Right, reading my answer back I am feeling silly. Obviously my suggestion would cause a browser timeout waiting for a response.

I really don't see a way of doing this other than with Ajax, unless there is some secret header that we don't know about that tells the browser not to expect a response.

tata668

5:05 pm on Jan 18, 2010 (gmt 0)

10+ Year Member




Obviously my suggestion would cause a browser timeout waiting for a response.

I don't think so. With "exit()" the response would be closed properly.


I really don't see a way of doing this other than with Ajax, unless there is some secret header that we don't know about that tells the browser not to expect a response.

Of course ajax would work. But this is not my question.

I don't think the way to accomplish what I'm trying to do (again just for fun) is to tell the browser it shouldn't expect any response. Because this would probably leads to a blank page, with the new url in the url bar (as with "exit()").

I think that if there is a way to do it, it will be by sending some kind of headers/content that would not be handled by the browser as "content to display".

Demaestro

6:46 pm on Jan 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think that if there is a way to do it, it will be by sending some kind of headers/content that would not be handled by the browser as "content to display"

Yes but in this case the browser would ask the user what it is to do with it as it would with any content it doesn't know how to handle.

If the user set up his browser to "Always do nothing" when you encounter this file/content type then maybe... but you would have to have this set up on every client machine that used the site and that isn't realistic unless it was an Intranet and the only machines looking at it were office workstations that you could set up in such a way.

tata668

7:07 pm on Jan 18, 2010 (gmt 0)

10+ Year Member



You are right on that Demaestro!

Now I see you understand what I'm really trying to find: some headers/content that would not be seen as "content to display" but that wouldn't trigger a "what do you want me to do with this content?" dialog box either! And without having to configure the browser so it automatically does something (or does nothing) with those particular headers/content either!

I'll repeat my main ideas:

- Finding a way to output javascript (text/javascript), or css (text/css), in a way the browser automatically interprets it.

- Finding a way to output an invalid attachment, in a way the browser wouldn't even pops a "save as" dialog box.

penders

11:32 pm on Jan 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Return an HTTP Status Code of 204 "No Content" - the calling page should go no where.

In test.php...

header("HTTP/1.1 204 No Content"); 
exit;

[w3.org...]

tata668

12:54 am on Jan 21, 2010 (gmt 0)

10+ Year Member



We have a winner!

penders's solution works!

I didn't know this header at all... Thanks!

phranque

8:23 am on Jan 24, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



in some cases the 304 Not Modified [w3.org] response would be appropriate and should have a similar result in the browser.