-1

i know, this looks stupid, but i am a new one in scripting so please help, i want my src not be ending with file with extension. like:

<script src="mysite.com/dir/"></script>

and NOT like this:

<script src="mysite.com/dir/script.js"></script>

is this done with HTACCESS or with what?

P.S. I want to create something like a hitcounter and dont want athers to see my script code


here is an sample:

 <script type="text/javascript" src="http://topsite.ucoz.com/stats"></script>

if you run this in html it will sent a mini banner and an hiden iframe with that page.

besKa88
  • 19
  • 3
  • 1
    What are you trying to solve by doing this? – Ignacio Vazquez-Abrams Oct 16 '11 at 22:19
  • Yes, that's (most likely) `.htaccess`. Anything else you'd like to know? – Bojangles Oct 16 '11 at 22:20
  • Are you trying to load some default js file under /dir/? – Ryan Oct 16 '11 at 22:20
  • but i've seen a lot of codes with dir-s in src :S – besKa88 Oct 16 '11 at 22:23
  • It is impossible to run JavaScript on a page but prevent viewing of the script. If it can be accessed by the ` – eyelidlessness Oct 16 '11 at 22:37
  • 1
    Just so you know, people can still see your code no matter what with javascript, even with your example. Use PHP or another server-side language. – xthexder Oct 16 '11 at 22:38
  • You can't really hide javascript. Even if you have set default javascript, client's browser will eventually download the script and execute it. This means that there is no point of hiding the filename in javascript link tag. – user482594 Oct 16 '11 at 22:49
  • If you have a big secret code, it must be kept on the server. If you just want to make it harder, you can compress your js files. The browser can run them, but they will be painful to understand for a developer. – Mic Oct 16 '11 at 22:57

5 Answers5

1

Use url rewrites in your .htaccess like this:

RewriteEngine On
RewriteRule ^dir/$ /dir/script.js [NC]

The problem with this is that /dir/ cannot be used for anything else.

What you could do is something like this:

RewriteEngine On
RewriteRule ^script/$ /dir/script.js [NC]

However this does not solve the problem of people seeing your code at all. The only solution to that is either code obfuscation or using php (or some other server-side language)

xthexder
  • 1,555
  • 10
  • 22
1

You can configure your web server to have a default page when you call a directory.

This is usually done for index.html, index.php, ... And here say this default is script.js the server should then deliver it by default

Mic
  • 24,812
  • 9
  • 57
  • 70
0

The first one is to a directory and not a file. It doesn't really make sense unless your thinking how on most servers http://abc/index.html goes to http://abc/

Cant really do it and cant see why you'd want to.

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
0

Either use the htaccess code listed to rewrite your url or simply fill your index.php with javascript.

and actually, using a url-rewrite would make it pointless having an index.php/index.html there in the first place.

mysite.com/page.php:

<script type="text/javascript" src="/dir/"></script>

mysite.com/dir/index.php:

alert('hello world');

also the sample you listed shows an example of someone using a file, not a whole directory. they simply leave off the extension to their file. Extensions only have semantic value.

Nacht
  • 3,342
  • 4
  • 26
  • 41
0

I think the main goal here is 'to hide javascript from the user' right?

If you want to hide a javascript, you may want to lookup for 'javascript obfuscation'.

You can't really hide javascript, because it must be able to run on the client browser. Even if you set the default url, as answers stated above, the javascript file will be downloaded.

However, you can obfuscate javascript files so that it will be hard for humans to read while machines can execute it.

There is a question in SO that ask about it.

How can I obfuscate (protect) JavaScript?

Community
  • 1
  • 1
user482594
  • 16,878
  • 21
  • 72
  • 108