PHP file_get_contents of HTML file requests old resources - javascript

I have a certain webpage. This is the file structure:
index.php
script.js
style.css
edit
|
|--- index.php
|
|--- script.js
edit/index.php is echoing the contents of index.php using file_get_contents('../index.php') which contains <script src='script.js'></script>. edit/script.js contains alert(). When I open https://www.example.com/edit, it displays the contents of index.php, but it doesn't execute the code inside edit/script.js. Why is this happening and how can I fix this? (Although index.php has the extension .php it doesn't contain any PHP, just HTML)

The problem is that the browser doesn't know that edit is actually edit/index.php. It thinks that this is a file in the root directory, so when parses script.js it looks for that in the root directory as well.
You should configure the webserver so that it performs a redirect from edit to edit/index.php, rather than just returning the contents of edit/index.php. This way, all relative URLs will be processed correctly. If you don't want to show the script name, you could just redirect to edit/ -- that's enough for it to know that this is a directory rather than a file.

Related

Loop to load all existing HTML files into one content

How can I load all html files in a folder into a content? I have about 125 html files in a folder src and need to load them in a content called .content.
$(".content").load("src/1.html");
is this doable on client side using JavaScript?
I belive you need to use PHP to get the numbers of files on a specific folder on your root directory.
http://php.net/manual/en/function.scandir.php
If you read this, you will be able to get the folder array by using PHP and they you can use JS or JQuery to do rest.

How to include a .js file into an html file that is served by a compojure server?

One of my html file needs to include a script file, as it often happens, and no matter what I do, the browser doesn't seem to get it.
following this answer: How to include css files into compojure project?
I created a public folder in my resource folder. The structure looks like this:
resources
|-public
|-views
| |-myview.html
|-scripts
|-my.script.js
Inclusion of the file in myview.html looks like this:
<head>
<script src="/scripts/my.script.js"></script>
</head>
When I request myview.html from the server I get it, but all of the types in my.script.js are unknown. The html works as expected when I just load it in the browser (I have to adjust the path to the script file to be relative, of course, and no, that doesn't work either when I request it from my server).
So how do I get my script files (and later css files) to be found by the html in a typical compojure setup?
Turns out I was just missing
(route/resources "/")
in my routing. As that wasn't the problem with the topic I looked at, I didn't figure it out for a while.
Turns out sometimes you should read the code in the question just as carefully as the answer...

Cannot get the js file under the static folder in Flask

It all works in my local server, but when others try to deploy what I have done to the server, it fails.
the file system is the server something like:
SERVER_FOLDER
--homepage
----static
----templates
------404.html
----app.py
----config.py
for example: The server is: MY_SERVER
and then in my app.py, I use
#app.route('/homepage/')
#app.route('/homepage/index')
def index():
# TODO
to define the homepage, and #app.errorhandler(404) to redirect all the not found page to 404.html
So I can get access to my homepage with http://MY_SERVER/homepage/, a little different than my local server. That's one thing that I am confused.
What I think is that the app.py runs under the MY_SERVER rather than MY_SERVER/homepage right?
But, in this way, when I run a template in my template file, and the html template file will use the js file under the static folder. the response always shows the js file is not found.
when I use <script src="{{ url_for('static', filename='file.js') }}"></script>, it shows not found in MY_SERVER/static and return to 404
when I try <script src="../homepage/static/file.js"></script>, same result.
How to handle this?
Build toward your solution:
Get flask serving image files from static
Put an image in the static directory and call it from your browser: http://yoursite/static/some_image_there.jpg
Plug away until that works.
Get flask serving the js file directly to your browser
Now put your js file into static and do as you did for the image.
Plug away until you can call it from the browser:
http://yoursite/static/yourfile.js
get your html to call the js file from static
Now you know that there is no problem actually serving the file, and you know the exact url to it. So it's not a big step to getting the HTML to reference it and your browser to load it.

Parent Directory '../' going up two directories

I am very confused.
I have been testing a site I am building on one server, and on that server I have a link to a javascript file. On said server I link to the file with '../js/javascript.js' and the file loads properly. On another server I just switched to, if I use the same link the file does not load. I look at firebug and see it seems that '../' is going up two directories and returning a 404 error.
I have fixed the path on the new server, but I am confused about the inconsistency.
I believe it has to do with the fact that I call the javascript in my header.php file, which is in a directory called inc. however the index.php where I include my header.php is in the root directory. But, I don't understand why on one server it references from the root and on the other it references from inside the inc directory.
site/
index.php
js/
javascript.js
inc/
header.php
It does not matter where header.php exists, because it gets included in index.php before the page is delivered to the browser. So any <script src="..."> tags will be relative to index.php. So, your HTML for including the js file should be:
<script src="js/javascript.js">
No ../! Not sure why it worked on the old server, but it definitely won't work on the new one as you've described it.

javascript file's order is not correct in drupal

Javascript file's order is not correct in Drupal. On other pages other than print page the order of script files are correct. The files are drupal.js and google_analytics_reports.js
Since the order is not correct it is causing error
"ReferenceError: Drupal is not defined"
Please help me to change the order of the files in Print pages(When we try to take a print out of the page, we will get this print page).
Are you sure that this is a problem of the js files order and not
something else?
Are the same js files loaded on print and normal pages?
What files are inside the /templates folder of your theme (or
generally files with tpl.php extension under your theme folder)?
If you just need a template for the printing mode install the print module and copy the print.tpl.php file into your theme templates folder. Then edit it as you prefer.

Categories

Resources