Conditional inclusion?

  • Thread starter Dr J R Stockton
  • Start date
D

Dr J R Stockton

My public Home Page contains (URL shortened) :

<script type="text/javascript" src="http://www.<etc.>"></script>

I would like to prevent any src being fetched when the page is viewed
locally as file:///?:/???/index.htm , to have it fetched and used when
viewed as http://www.merlyn.demon.co.uk/index.htm , and I don't much
care what happens if someone takes a copy of the page and views that.

When viewing from file, I don't mind what that part displays, provided
it is small and the rest of the page is not affected.

I know how to detect, in JavaScript, whether I want that src fetched.

Any suggestions, preferably tested ones?
 
J

Jorge

My public Home Page contains (URL shortened) :

   <script type="text/javascript" src="http://www.<etc.>"></script>

I would like to prevent any src being fetched when the page is viewed
locally as   file:///?:/???/index.htm , to have it fetched and used when
viewed as  http://www.merlyn.demon.co.uk/index.htm, and I don't much
care what happens if someone takes a copy of the page and views that.

(...)

Any suggestions, preferably tested ones?

Use relative paths: remove the "http://www.merlyn.demon.co.uk/" part
of the url.
You can move backwards in the file tree with "../" or "../../" etc.
For example:

If the file @ "http://www.merlyn.demon.co.uk/examples/index.html" uses
the script @ "http://www.merlyn.demon.co.uk/scripts/js.js", you can
refer to that .js from within that .html like this: <script src="../
scripts/js.js"></script>
 
R

RoLo

My public Home Page contains (URL shortened) :

   <script type="text/javascript" src="http://www.<etc.>"></script>

I would like to prevent any src being fetched when the page is viewed
locally as   file:///?:/???/index.htm , to have it fetched and used when
viewed as  http://www.merlyn.demon.co.uk/index.htm, and I don't much
care what happens if someone takes a copy of the page and views that.

When viewing from file, I don't mind what that part displays, provided
it is small and the rest of the page is not affected.

I know how to detect, in JavaScript, whether I want that src fetched.

Any suggestions, preferably tested ones?

--
 (c) John Stockton, nr London, UK. [email protected]  Turnpike v6.05  MIME.
 Web  <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms& links;
  Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
 No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

to be clear...
your asking only tags that use src like img, script or iframe and not
tags like <a> that uses href? or only scripts?
your asking in case someone saves your page to a local file, so it
doesn't fetch the src if the browser doesn't store them also?

!!NOT TESTED!!

I belive you will have to make a list of the tag's, detect if the
browser is been viewed from file: or http: in location.href . If its
file, don't apply the src's else apply them.

Since the browsers will try to fetch them as soon their read.

Or you could try the <script defer="defer" src=".."/> (Only works on
IE) and set a script at the end of the page to remove the not desired
script's...
defer should delay the execution of the script until the page loads...
I'm not sure if it will prevent the fetch tho...

Rolo
 
T

Thomas 'PointedEars' Lahn

Dr said:
My public Home Page contains (URL shortened) :

<script type="text/javascript" src="http://www.<etc.>"></script>

I would like to prevent any src being fetched when the page is viewed
locally as file:///?:/???/index.htm , to have it fetched and used when
viewed as http://www.merlyn.demon.co.uk/index.htm , and I don't much
care what happens if someone takes a copy of the page and views that.

When viewing from file, I don't mind what that part displays, provided
it is small and the rest of the page is not affected.

[...]
Any suggestions, preferably tested ones?

Several possibilities come to mind:

1. Client-side conditional dynamic loading of script resources by inserting
`script' elements or eval()ing XHR responses (if necessary, this should
work over `file://' URIs also; IE/MSHTML-based browsers might need a bit
of tuning, though).

2. A client-side proxy script, written in a supported ECMAScript
implementation (e.g. in JavaScript for Netscape or Gecko-based
browsers), that prevents fetching certain resources over HTTP(S)
(or redirects to a replacement dummy) when the including resource
is retrieved with a `file://' URI.

3. Server-side parsing of .htm resources, e.g. <?php echo '<script ...'; ?>
which would probably be ignored client-side due to error correction
(unsupported/invalid Processing Instruction).

4. Server-Side Includes (SSI).

Of those, SSI seem the best way to go, for the other possibilities are
either unreliable, may not be feasible, or have unknown side effects. In
contrast, SSI should not pose a problem if they don't contain `-' (or `--'),
and they run only if a Web server is serving the file that has SSI support
enabled; with `file://' URIs they are just comments in SGML-based documents:

<!--#set var="etc" value="&lt;script type=&quot;text/javascript&quot;
src=&quot;http://www.etc.&quot;&gt;&lt;/script&gt;" -->
<!--#echo var="etc" -->

Untested. See also: <http://en.wikipedia.org/wiki/Server_Side_Includes>


HTH

PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <b1bfc494-6906-43f3-9478-f954df5ed3f6@v3
9g2000pro.googlegroups.com>, Sun, 1 Feb 2009 10:32:39, Jorge
Use relative paths: remove the "http://www.merlyn.demon.co.uk/" part
of the url.

There is no such part. Attribute src refers to a distant site; if it
were local, there would be no point in preventing it from being loaded,
and controlling its loading would be easy.
You can move backwards in the file tree with "../" or "../../" etc.

Of course.

SSI and PHP cannot be used for this.
DSS.


However, it has since occurred to me that, since the remote URL, while
itself .xml, requires JavaScript to do anything, I can rely on remote
JavaScript to invoke it, and so use document.write for the above script.
So the matter appears solved; the item will appear only if the page
comes from my present server, or one that presents pages that pass the
server test (possibly the Wayback Machine).

OK in IE7, FF3, Op8, Sf3, apart from failed centring in IE7, which does
not matter.
 

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

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,646
Members
47,253
Latest member
AntwanNews

Latest Threads

Top