Loading html with ajax, and relative paths - javascript

I use ajax to load html files when the links on my page are clicked. Here is the simplified version of what's going on:
links.click(function () {
var href = $(this).attr("href");
history.pushState(null, null, href);
$.get($(this).attr("href"), function(data){
$("#page").html(data)
})
});
This works fine. However, the loaded .html files contain a lot of images with relative paths, as follows:
<img src="smiley.gif">
Therefore, when the files are loaded, the text of the page is okay but none of the relative content is being loaded properly. Creating my images with absolute paths solve my problem however I need them to be relative for my current case.
As you've noticed I've modified the address bar by using history.pushstate in my click event function. I assumed that the loaded url would load all of the resources properly as the url is also modified. However, it's not working like that with the code above.
What can I do to fix this issue in an elegant and simple way?

What you want is to insert a base tag in your resulting html, to specify where to root of all relative paths should be at.
So lets say that all your relative paths should be served from /assets and you load /user/1/profile, a relative image with: <img src="nyan.gif" /> would be checked at /user/1/profile/nyan.gif and will be returned invalid. If you however insert a <base href="/assets"> in the head tag, the image will be served correctly

It's not clear to see what the URL you are on, which is ultimately going to affect how your page reacts.
For instance if you are on a page that looks like www.example.com/products and you try to load smiley.gif that's going to end up being www.example.com/smiley.gif.
If instead you are on www.example.com/products/ (NOTE: trailing slash) or www.example.com/products/index.html however, smiley.gif would point to www.example.com/products/smiley.gif. You don't know what URL the user is going to type in, but you could:
update your links to include the final slash to mitigate this, and
forward the user to a location with the trailing slash if they type it in incorrectly.
One slash on the end of your URL may be all that you need to fix this but... you may want to consider putting all your asset files into one directory so you don't have to play this game at all! /images/smiley.gif could end up being easier for you in this case.

Related

How to remove the inserted page number from url in Gatsby

I am making a blog with Gatsby and have created "paginated pages" in the gatsby-node.js file as shown in the image. This makes a new page after a certain number of posts.
Then in my templates/allPosts.js file I map through the posts and have a <Link to={frontmatter.slug} /> for each post. All the links work fine on the first page but after that e.g. on page 2, the page number is inserted into the URL before the slug, but this is not the correct URL for the blog article. I need the page number to not be inserted.
Creating paginated pages in the gatsby-node.js file
Based on the section in the Gatsby docs on relative links you should be able to prefix your slugs with /.. to remove the page component in your urls.
The <Link /> component follows the behavior of #reach/router by ignoring trailing slashes and treating each page as if it were a directory when resolving relative links. For example if you are on either /blog/my-great-page or /blog/my-great-page/ (note the trailing slash), a link to ../second-page will take you to /blog/second-page.
To keep things consistent, you might want to add the page number to the first page too.
So, according to what you said, you want to keep the page structure (i.e: /blog/number/) but the post slug must remove the number, isn't?
Then, on the link to the post, use an absolute URL like: <Link to={`/blog/page-title`}> instead of <Link to={`page-title`}>. Note that if you want to remove and use absolute paths you must prefix the links with a slash (/). You can also check (because there's no code in your question) if using relative paths such as ../post-title works for you.
You may want to take a look at Gatsby's documentation about relative links.

How to dynamically change href attribute

I'm trying to make a little website browsable both online and offline using only html, css and a little of jquery\javascript.
Hence I'm using all relative paths and everything works fine unless I came to the issue to load a custom menu in all my pages with a little smart jquery include.
However since my menu.html is loaded in different pages located in different subdirectories of the tree structure I am wondering what's the smartest way to write down the href links of the different voices in the menu.
I initially started using all absolute paths in the menu.html, but of course it just works only online or offline based on which root domain I use in the absolutes paths (either http://mywebsite.com/ or file:///D:myfolder/etc).
Of course also using the / at the beginning of a link works only online, since locally the / stands for the drive letter where the websites' folder is placed and it will work if and only if the website's folder is saved in the highest path like as D:/myWenbsite. I'd like to make something more adaptable regardless of the local path.
The best way in my opinion is to use relative URL's from the root. For example in your menu.html file when you reference jquery you can do the following:
/javascript/jquery.min.js
Adding the beginning '/' makes it so that the path always starts from the root of the domain no matter where your html is at in your directory.
If you used:
javascript/jquery.min.js
That means in whatever directory your menu.html file is in, a folder for javascript would also need to exist and that is not generally wanted.
Using the <base> command within a little script to change it solved my issue.
Here is an example:
<head>
<!-- Here a direct path is need to firstly load jquery -->
<script type = "text/javascript" src = "../include/js/jquery-1.10.2.min.js"></script>
<base id="host" href="" />
<script>
/* Where am I? */
here = window.location.href;
hereIndex = here.indexOf('temp-test');
/* make substring from root till temp-test/ */
newPathname = here.substring(0, hereIndex+10); //+10 to consdier also temp-test/
$("#host").attr("href", newPathname);
</script>
</head>
Don't know if there is a better way to do it.
Anyway even if the page renders correctly in the console log I still get errors on every relative path I have GET file:///D:/temp-test/core/image/temp1.jpg net::ERR_FILE_NOT_FOUND however for instance, this image is instead properly loaded. So what's up here with the base tag? It is kinda of not getting recognized but it works..
Further investigation is needed for me.

Is there a way to set window.location so that relative paths follow this instead of what is given by default window.location

I have a new server and I have existing files with relative links in CSS, img tags, etc. How do i set my own window.location to be followed by such files, I am not looking for anything hectic in particular just for within my website. e.g http://stackoverflow.com and https://stackoverflow.com/questions/ I need the latter to be my site-url. I am thinking in the lines of javascript but any solution is welcome.
Maybe you will be able to use the <base>-tag
<base href="http://www.example.com/">
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

Mobile changePage Url adding root directories

Everytime I click to load the video in the new page, the url parameters are getting the base then repeating themselves.
So it goes from /screen/mobile/videos.asp to /screen/mobile/videos.asp#/screen/mobile/videoplayers.asp?id=b7c5z654vz_ak0
I've played around with it so much, but it seems no matter what I do it always resorts to the above, even when using absolute urls.
$(".videolink").unbind("click").click(function(e) {
e.preventDefault();
var data = $(this).attr("href");
$.mobile.changePage("videoplayer.asp?id="+data);
console.log('changing to videplayer.asp');
});
Need to find a work around to it as the urls are not friendly at all.
Did you try specifying an absolute URL? For example,
$.mobile.changePage("/videoplayer.asp?id="+data);
Whenever JQM loads pages with AJAX it will do that - since it only appends the new div to the current DOM (it's not actually reloading the full page).
If you want to disable this AJAX loading (and actually load a fresh page into the browser), you need to look into the documentation for example the AjaxEnabled property for setting this globally.
Hope this helps...

html image src confusion

If I put in the src attribute ./images/nothing.gif what does that translate to?
I have a javascript file that makes src attribute of some html element to 'nothing.gif'
but on the page nothing.gif shows as 'file not found' symbol.
Currently nothing.gif resides at the following place in my ftp server:
/www/foldername/wp-content/themes/themeg/images/nothing.gif
the javascript resides at:
/www/darksnippets/wp-content/themes/themeg/javascript.js
since this is wordpress there is no actual 'html page' the content is stored in the DB. so If I used ../ where should I place nothing.gif?
Edit:
here is the link to the page: http://www.darksnippets.com/?page_id=56
nothing.gif can be found here: http://www.darksnippets.com/wp-content/uploads/nothing.gif
in the bottom right you will see broken image symbol (this shows up in IE of Chrome. does not show in FF)
The relative path ./images/nothing.gif is interpreted by the browser, not the server. So it will look at the url from the browser's perspective to resolve the path. What is the url that the browser sees?
Update:
I see you've provided URLs. Change your relative path to:
./wp-content/uploads/nothing.gif
But a better solution would be to use a root relative path. i.e. one that starts with a /
If the HTML page is http://www.darksnippets.com/?page%5Fid=56 then nothing.gif points to http://www.darksnippets.com/nothing.gif. The location of the Javascript is not relevant. So you should just need "wp-content/uploads/nothing.gif".
One of the ideas is to use a full path to the image file
e.g. http://www.yourdomain.com/images/nothing.gif
Not having worked with wp very much, I can't give a specific answer, but a general solution would to be to use the format "http://www.sitename.com/folder/nothing.gif" Where, of course, sitename.com/folder gets replaced with your domain name and the folder on your site.
If you're writing a Wordpress template in PHP, you can access the full path to your template directory with:
bloginfo('template_directory')
Alternatively, if you can set the images as background images via CSS (instead of using src), relative image paths defined in CSS will be relative to the CSS path:
.nothing { background: url(images/nothing.gif); }
As I thought, in IE and Firefox the browser is looking for http://www.darksnippets.com/images/nothing.gif. The relative URL is relative to www.darksnippets.com (the url of the page), not to the javascript's location.
Ates Goral's answer is correct. I'll expand to address other questions you raised.
In relative paths, . (single dot) refers to the current path. This will generally be the path of the page loaded (and resolved according to the rules of your web server), unless your page uses a <base href="..."> which is different from your current path. This is because every page loaded has a base path, which defaults to... you guessed it, . (single dot). Likewise, .. (two dots) refers to the parent directory of the current path (also resolved according to rules on your web server).
For this problem you can simply remove the dot(.) and / sign from your code
So now your code will be like
src = images/nothing.gif
I think this will work for you.

Categories