Javascript makes the current directory to change?

M

Mateusz Viste

Hi,

I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)...

I recently put the following script online:
<script type="text/javascript"><!--
function playmedia(mediafile) {
newwindow=window.open();
if (window.focus) {newwindow.focus()}
newwindow.document.write('<html>');
newwindow.document.write(' <head>');
newwindow.document.write(' <title>Media player</title>');
newwindow.document.write(' </head>');
newwindow.document.write(' <body bgcolor="#A0A0FF" text="#000000">');
newwindow.document.write(' <center>');
newwindow.document.write(' <h2>Playing the media file... ♫</h2><br>');
newwindow.document.write(' <!-- show play button, autostart and loop once -->');
newwindow.document.write(' <embed src="' + mediafile + '" hidden=false autostart=true loop=1 autosize=1>');
newwindow.document.write(' </center>');
newwindow.document.write(' </body>');
newwindow.document.write('</html>');
newwindow.document.close();
}
//--></script>


I call this script using the following link:
<a href="errorJS.htm" onclick="playmedia('../goodies/antarcti/antarcti.mid'); return false;">Play</a>

So far, so good: the script is working, and I do not have any problem.
Troubles comes when I want to use my website locally (I often gave an offline copy to my relatives, as they prefer to use it offline). When used from the disk, the browser (Fx) can't find the file. If I change the link from playmedia('../goodies/antarcti/antarcti.mid') to playmedia('goodies/antarcti/antarcti.mid'), then it works offline, but when put on the server I get an 404 :(
I'm using frames, too. The main frame is in / , sub-frames are in /html/ , and the media files are stored in /goodies/. It seems that in some circumstances, the "current" directory isn't the same...

Now the big question comes: Is this some Javascript-related issue? If not, what could be the problem? The browser? My web server?

The website is at http://viste.homeip.net/mateusz/nes

Best regards,
Mateusz Viste
 
S

SAM

Mateusz Viste a écrit :
Hi,

I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)...

I thhink you have to learn about absolute and relative links ...
In relative you have 2 possibilities
- relative to the file : href="imag.jpg" href="../../folder/img.jpg"
- relative to root of site : href="/folder/images/img.jpg"

You seem to use relative link alternatively locally and on server,
But, apparently you want to use relative to the file (and not to the
root of site).

So the script must be in the correct folder on server and locally
that's to say in a folder on same floor than folder 'goodies'

[folder or site]
[nes]
[htm_roms]
file : popup.htm (with the script)
(or linked to the script at any place)
[goodies]
[antarcti]
file : antarcti.mid

or,
on server (site = http://viste.homeip.net/mateusz/) :
site/nes/htm_roms/popup.htm
site/nes/goodies/antarcti/antarcti.mid
on PC :
C:Documents/Mysite/nes/htm_roms/popup.htm
C:Documents/Mysite/nes/goodies/antarcti/antarcti.mid

If all that seems correct to you at home and on site,
look at precisely the names of your folders and files in both places.
It's possible that some of them has(have) their 1st letter in uppercase.
At home there is no importance, on site that's count (antarcti not same
as Antarcti) and href must reflects the correct spelling.
 
M

Mateusz Viste

Ð’ ВоÑкреÑенье 07 ÑентÑÐ±Ñ€Ñ 2008 15:58, SAM пиÑал:
I thhink you have to learn about absolute and relative links ...
In relative you have 2 possibilities
- relative to the file : href="imag.jpg" href="../../folder/img.jpg"
- relative to root of site : href="/folder/images/img.jpg"

Yes, I exactly know what you are talking about :)
You seem to use relative link alternatively locally and on server,
But, apparently you want to use relative to the file (and not to the
root of site).

I am always using relative links. That allows me to use the website as an
online page, and to copy it easily to other computers without any problem.
So the script must be in the correct folder on server and locally
that's to say in a folder on same floor than folder 'goodies'

Agin, you are 100% right ;-)
If all that seems correct to you at home and on site,
look at precisely the names of your folders and files in both places.

Names are correct. By the way, both my web server and my PC are
case-sensitive (I am using Linux on both).

The problem is the following:
On my local PC, the script needs to be called like that:
playmedia('goodies/antarcti/antarcti.mid')

Just like it would be in the root directory (in fact, it is not, but at the
begin I assumed that JavaScript is acting like that).

When I put the same script/website on my web server, my browser (the same I
used locally - Firefox 3.0.1) is acting like the current directory wasn't
the same anymore:

GET /mateusz/nes/htm_roms/goodies/advisln3/advtitle.mid HTTP/1.1

Which is not correct, as the root of the website is /mateusz/nes , and the
media file is in /mateusz/nes/goodies/advisln3/advtitle.mid :(

And that is the point which I definetely doesn't understand - why my browser
is changing the reference path when used locally... I just don't really
know where I have to search - is it a Firefox bug, something
JavaScript-specific, anything else...? But I guess it's offtopic for this
group anyway :)

Best regards,
Mateusz Viste
 
S

SAM

Mateusz Viste a écrit :
The problem is the following:
On my local PC, the script needs to be called like that:
playmedia('goodies/antarcti/antarcti.mid')

Just like it would be in the root directory (in fact, it is not, but at the
begin I assumed that JavaScript is acting like that).

Not at all !
That means folder 'goodies' is in same folder than the file that opens
the popup. (you certainly have a copy of 'goodies' or an alias of it in
'htm_roms' on your PC, or you did get one and Fx uses its cache)

the file with the script could be here : /nes/htm_roms/
when midi file is here : /nes/htm_roms/goodies/antarcti/

The JS follows relative links from the page where it is declared
(or linked), it could not happen any error about the hrefs it they are
set like in the page in html code.
When I put the same script/website on my web server, my browser (the same I
used locally - Firefox 3.0.1) is acting like the current directory wasn't
the same anymore:

GET /mateusz/nes/htm_roms/goodies/advisln3/advtitle.mid HTTP/1.1

That url is correct if the html file in 'htm_roms' calls :
'goodies/antarcti/antarcti.mid'
Which is not correct, as the root of the website is /mateusz/nes ,

more probably the exact root is : /mateusz/ (href="/")
/nes/ is a kind of sub-site (its relative root is : href="/nes/" )
and the
media file is in /mateusz/nes/goodies/advisln3/advtitle.mid :(

Right ! :)
And that is the point which I definetely doesn't understand - why my browser
is changing the reference path when used locally...

No it doesn't, it can't !

delete your folder 'goodies' in 'htm_roms' on your PC
(or move it back to 'nes')
don't know where I have to search - is it a Firefox bug, something
JavaScript-specific, anything else...? But I guess it's offtopic for this
group anyway :)

Well, I see here, in your page :
<http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm>
with the code (correct as 'htm_roms' is with 'goodies' in 'nes'):

<a href="../goodies/advisln3/advtitle.mid" target="_blank">
Adventure Island 3 title music theme</a>

In the head of this page you could have :

<script type="text/javascript" src="../js/sound.js"></script>

Then you save in folder /nes/js/
the file 'sound.js' containing exactly :

function pop(what) {
if(typeof nw = 'undefined' || nw.closed)
nw = window.open('','','width=300,height=60,resizable=1');
nw.location = what.href; // to avoid any mistake
nw.focus();
return false;
}

Then your link becomes :

<a href="../goodies/advisln3/advtitle.mid" target="_blank"
onclick="return pop(this);" title="To listen the musical theme">
Adventure Island 3 title music theme</a>


=================
You can also have the script in your menu's page
<http://viste.homeip.net/mateusz/nes/html/index.htm>

<head>
<title>NES games repository</title>
<meta
(...)
<script type="text/javascript" src="../js/sound.js"></script>
</head>

The opened pages in right frame no more have JS's script
and their links to play sound are this way :

<a href="../goodies/advisln3/advtitle.mid" target="_blank"
onclick="return parent.index.pop(this);"
title="To listen the musical theme">
Adventure Island 3 title music theme</a>


=================
You can also have the script in your main page :
<http://viste.homeip.net/mateusz/nes/index.htm>

<script type="text/javascript" src="js/sound.js"></script>
</head>

Right pages will be without JS and with links this kind :

<a href="../goodies/advisln3/advtitle.mid" target="_blank"
onclick="return parent.pop(this);"
title="To listen the musical theme">
Adventure Island 3 title music theme</a>
 
M

Mateusz Viste

Hi :)

Ð’ ВоÑкреÑенье 07 ÑентÑÐ±Ñ€Ñ 2008 20:02, SAM пиÑал:
That means folder 'goodies' is in same folder than the file that opens
the popup. (you certainly have a copy of 'goodies' or an alias of it in
'htm_roms' on your PC, or you did get one and Fx uses its cache)

No, there was never any "goodies" directory in "htm_roms"...
the file with the script could be here : /nes/htm_roms/

That's true. Well, in fact, that's the location of the script which
The JS follows relative links from the page where it is declared
(or linked), it could not happen any error about the hrefs it they are
set like in the page in html code.

That what I believed in until today. :)
That url is correct if the html file in 'htm_roms' calls :
'goodies/antarcti/antarcti.mid'

No, there is no goodies in htm_roms. The essential question which is black
magic for me, is why Firefox tries to find the media
in /mateusz/nes/htm_roms/goodies/advisln3/advtitle.mid, but when runned
locally it finds it just fine in /mateusz/nes/goodies/advisln3/advtitle.mid
(I am talking about the first version of my script, which doesn't
added "..". The current version, which is online, do works fine remotely,
but doesn't locally).

Just try to save the page
http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm and the file
http://viste.homeip.net/mateusz/nes/goodies/advisln3/advtitle.mid to your
disk (locally - just pay attention to respect relative links), and try to
run it. It won't (at least for me it is not - I have to remove "../" to
make it work).
more probably the exact root is : /mateusz/ (href="/")
/nes/ is a kind of sub-site (its relative root is : href="/nes/" )

Yes, these are subdirectories. I tried to put it directly into /, but it
isn't changing anything.
delete your folder 'goodies' in 'htm_roms' on your PC
(or move it back to 'nes')

There never was such... :-D
The stuff which is online is an exact copy of what I have on disk.
That's the whole point: the same version of the code doesn't work locally
and remotely. It works locally OR remotely, depending on how I generate my
script.
Well, I see here, in your page :
<http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm>
with the code (correct as 'htm_roms' is with 'goodies' in 'nes'):

<a href="../goodies/advisln3/advtitle.mid" target="_blank">
Adventure Island 3 title music theme</a>
True.

In the head of this page you could have :

<script type="text/javascript" src="../js/sound.js"></script>

Then you save in folder /nes/js/
the file 'sound.js' containing exactly :

That seems to be some very clever code. I will have to read it several times
tomorrow to understand what is it about :)
Is it some routine which detects the current path of the program, allowing
to not bother about relative paths anymore?

Anyway, much thanks for your investigations :)

Best regards,
Mateusz Viste
 
S

SAM

Mateusz Viste a écrit :
The current version, which is online, do works fine remotely,
but doesn't locally).

Just try to save the page
http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm and the file
http://viste.homeip.net/mateusz/nes/goodies/advisln3/advtitle.mid to your
disk (locally - just pay attention to respect relative links), and try to
run it. It won't (at least for me it is not - I have to remove "../" to
make it work).

I'm sorry, downloaded in correct folders your files as you can see :
<http://cjoint.com/?jhxsPergjb>
All run as expected (locally) when I click 'Play'
There never was such... :-D
The stuff which is online is an exact copy of what I have on disk.

I can't believe you, or your Linux is playing a jock to you.
(do you use the correct url to open the file locally ?)
(drag the file and drop it in the Firefox's window)

At least, did you try locally with another browser ?
That's the whole point: the same version of the code doesn't work locally
and remotely. It works locally OR remotely, depending on how I generate my
script.

Please try (locally and on site) this version of your html file
(donwload it at the right place !):
<http://cjoint.com/data/jhxECGhbPf_advisln3_asm.htm>
and click the link : 'Adventure Island 3 title music theme'
(there is another script with automatic src for the embed)

Don't try it on the site cjoint.com
That seems to be some very clever code. I will have to read it several times
tomorrow to understand what is it about :)

It is about external JS file linked to each page needing this code.
The JS code will be put in browser's cache, downloaded only once for all
the pages
Is it some routine which detects the current path of the program, allowing
to not bother about relative paths anymore?

It use the href of the link to give it to embed's src.
If the link works when JS is disabled, the popup must (and has to) work
too if JS is enabled.
Anyway, much thanks for your investigations :)

You can say it ! :)
 
M

Mateusz Viste

Ð’ ВоÑкреÑенье 07 ÑентÑÐ±Ñ€Ñ 2008 23:52, SAM пиÑал:
I'm sorry, downloaded in correct folders your files as you can see :
<http://cjoint.com/?jhxsPergjb>
All run as expected (locally) when I click 'Play'

Yes, I know :)
I have done the same, and that works. Therefore I localized what exactly
make it to not work - frames. When I download the following files:
http://viste.homeip.net/mateusz/nes/index.htm
http://viste.homeip.net/mateusz/nes/html/index.htm
http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm
http://viste.homeip.net/mateusz/nes/goodies/advisln3/advtitle.mid

and launch the main, big (containing 2 frames) index.htm, then it doesn't
work locally.
When run locally, Firefox takes the href of the main "frame container", when
run remotely, it takes the href of the child frame.
I can't believe you, or your Linux is playing a jock to you.

I know it looks bad :p
Now, I am 99% sure that the "joke" comes from Firefox 3. I tried today at
work, on a Windows machine, and when used locally, it was almost working. I
wrote "almost", because the MS media player told "root could have been
changed" and don't wanted to play the file, but it was able to find it at
all. That made me curious, so I downgraded my Firefox at home from v3.0.1
to v2, and IT WORKS. So my sentence is: Firefox 3 sux :-D
(do you use the correct url to open the file locally ?)
(drag the file and drop it in the Firefox's window)

Please trust me - I'm not *that* dumb. ;-) In fact, I work as a network
engineer, I'm just crap when it comes to JavaScript stuff :)
At least, did you try locally with another browser ?

As I wrote, I tried today at work. I can't really test with another browser
Please try (locally and on site) this version of your html file
(donwload it at the right place !):
<http://cjoint.com/data/jhxECGhbPf_advisln3_asm.htm>
and click the link : 'Adventure Island 3 title music theme'
(there is another script with automatic src for the embed)

I downloaded it (using wget, to be sure that it won't be modified on the
fly), I replaced my previous advisln3.htm file, and it doesn't work either.

I think that discussion may be ended here.

I am really grateful for the great help you had the kindness to provide to
me. Many thanks for your friendly advices, it made be find the faulty part
of the puzzle much quicker than if I had to troubleshoot it by myself (in
fact, I was 75% sure that the problem was in the JavaScript code, so I
would have a very very long way to go) :)

Best regards,
Mateusz Viste
 
S

SAM

Mateusz Viste a écrit :
Ð’ ВоÑкреÑенье 07 ÑентÑÐ±Ñ€Ñ 2008 23:52, SAM пиÑал:
Therefore I localized what exactly
make it to not work - frames. When I download the following files:
http://viste.homeip.net/mateusz/nes/index.htm
http://viste.homeip.net/mateusz/nes/html/index.htm
http://viste.homeip.net/mateusz/nes/htm_roms/advisln3.htm
http://viste.homeip.net/mateusz/nes/goodies/advisln3/advtitle.mid

and launch the main, big (containing 2 frames) index.htm, then it doesn't
work locally.
When run locally, Firefox takes the href of the main "frame container",

Tremendous ! that's true :-(
so I downgraded my Firefox at home from v3.0.1
to v2, and IT WORKS. So my sentence is: Firefox 3 sux :-D
;-(

As I wrote, I tried today at work. I can't really test with another browser
on my own machine, as I does not have any multimedia plugins for Opera nor
Konqueror (Firefox is the only one which is able to play <embed> files).
Anyway, it doesn't matter anymore.

And IE ?
<http://www.tatanka.com.br/ies4linux/>
(don't play midi files on my ies4osx (Mac) but Flash seems OK)

I downloaded it (using wget, to be sure that it won't be modified on the
fly), I replaced my previous advisln3.htm file, and it doesn't work either.

It does locally in framed page with Fx.3.
I mean : the left link (Adventure Island 3 title music theme)
I think that discussion may be ended here.

Yeap !

SYL
 
M

Mateusz Viste

Just for future generations, here is a version of the script which works both remotely and locally on Firefox. The solution has been kindly given to me by SAM ;-)

First, we have to declare the script in the <head> section of the page:

<script type="text/javascript"><!--
function playmedia(mediafile) {
newwindow=window.open();
if (window.focus) {newwindow.focus()}
newwindow.document.write('<html>');
newwindow.document.write(' <head>');
newwindow.document.write(' <title>Media player</title>');
newwindow.document.write(' </head>');
newwindow.document.write(' <body bgcolor="#A0A0FF" text="#000000">');
newwindow.document.write(' <center>');
newwindow.document.write(' <h2>Playing the media file... ♫</h2><br>');
newwindow.document.write(' <!-- show play button, autostart and loop once -->');
newwindow.document.write(' <embed src="' + mediafile + '" hidden=false autostart=true loop=1 autosize=1>');
newwindow.document.write(' </center>');
newwindow.document.write(' </body>');
newwindow.document.write('</html>');
newwindow.document.close();
}
//--></script>

Then, we call it using the following link:

<a href="../goodies/chipdale/speedrun.flv" onclick="playmedia(this.href); return false;">Play</a>

Mateusz Viste
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top