简体   繁体   English

用ajax和相对路径加载html

[英]Loading html with ajax, and relative paths

I use ajax to load html files when the links on my page are clicked. 单击页面上的链接时,我使用ajax加载html文件。 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: 但是,加载的.html文件包含许多具有相对路径的图像,如下所示:

<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. 如您所见,我已经通过在click事件函数中使用history.pushstate修改了地址栏。 I assumed that the loaded url would load all of the resources properly as the url is also modified. 我假设加载的url可以正确加载所有资源,因为该url也被修改了。 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? 我该如何以一种优雅而简单的方式解决此问题?

It's not clear to see what the URL you are on, which is ultimately going to affect how your page reacts. 目前尚不清楚您所使用的URL,这最终会影响您的页面反应。

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 . 例如,如果您在一个看起来像www.example.com/products的页面上,而您尝试加载smiley.gif,那么最终将是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 . 相反,如果您使用的是www.example.com/products/ (注意:斜杠)或www.example.com/products/index.html ,则smiley.gif将指向www.example.com/products/smiley.gif You don't know what URL the user is going to type in, but you could: 您不知道用户要输入什么URL,但是您可以:

  1. update your links to include the final slash to mitigate this, and 更新您的链接以包括最后的斜杠以减轻这种情况,并且
  2. 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! URL末尾的斜线可能是解决此问题所需的全部,但是...您可能需要考虑将所有资产文件都放在一个目录中,因此根本不必玩这个游戏! /images/smiley.gif could end up being easier for you in this case. 在这种情况下,/ /images/smiley.gif可能最终使您更轻松。

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. 您想要的是在结果html中插入一个基本标记 ,以指定所有相对路径的根目录应该在哪里。

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. 因此,可以说应该从/assets提供所有相对路径,然后加载/user/1/profile ,将在/user/1/profile/nyan.gif检查具有以下内容的相对图像: <img src="nyan.gif" /> /user/1/profile/nyan.gif并且将返回无效值。 If you however insert a <base href="/assets"> in the head tag, the image will be served correctly 但是,如果您在head标签中插入<base href="/assets"> ,则会正确投放图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM