AJAX改变页面内容
我已经使用 AJAX 成功地更改了网页的内容。我可以包含我的域中的另一个网页,但我遇到的问题是使超链接正常工作。如果超链接使用相对寻址,那么相对于我将其包含在内的页面将不起作用,因此我正在研究 php 来解析 html 标签,因为我在中读取它,
我正在使用以下 RegExp /href[\s] ?=[\s\"\']+(.*?)[\"\']/im
查找 href 数据,但想要一个关于如何为相对地址添加前缀的指针。
我想替换链接href="./test1/page1.html"
在页面http://foo.bar.com/folder1/info1/data.html with href="http://foo.bar.com/folder1/info1/./test1/page1.html"
那么如果我包含 /folder1 的页面内容http://foo./info1/data.html bar.com/folder2/faraway/another.html 嵌入页面上的链接将正常工作 我正在考虑使用 php preg_replace 函数来做到这一点,但很快就失败了。如果我找错了树,并且有更合适的工具或方法,有人可以指出我正确的方向吗;-)。也许这一切都可以用 Javascript 来实现?
I have used AJAX to successfully change the content of a web page. I can include another web page from my domain but the problem I have is making the hyperlinks to work. If the hyperlinks use relative addressing then that will not work relative to the page I am including it in so I was investigating php to parse the html tag as I read it in
I am using the following RegExp /href[\s]?=[\s\"\']+(.*?)[\"\']/im
to find the href data but would like a pointer on how I can prefix a relative address.
I would like to replace a link href="./test1/page1.html"
on page http: // foo.bar.com/folder1/info1/data.html
with href="http: // foo.bar.com/folder1/info1/./test1/page1.html"
then if I include the the page content of /folder1/info1/data.html in http://foo.bar.com/folder2/faraway/another.html the links on the embedded page will function correctly
I was looking at using the php preg_replace function to do that but have very quickly come unstuck. If I am barking up the wrong tree and there is a more appropriate tool or approach can someone please point me in the right direction ;-). Maybe it can all be down in Javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您计划在页面上执行更多 JavaScript,则可以使用 JQuery。
将“#embedded”替换为嵌入页面的 ID。
如果您不打算使用 javascript 来做其他事情,那么这几乎肯定是矫枉过正,但如果您打算制作一个闪亮的动态 ajaxy 页面,您可能会考虑一下。
奖金:
使用 JQuery 进行 ajax 页面加载:
If you're planning to do much more javascript on the page, you could use JQuery.
Replace "#embedded" with the id of your embedded page.
This is nearly certainly overkill if you're not going to use javascript for anything else, but if you're planning to make a shiny dynamic ajaxy page, you might look into it.
Bonus:
Doing ajax page loading with JQuery:
进一步考虑 ABentSpoon 的响应,您的 jQuery 选择器可以搜索所有以斜线开头的锚标记。
有关 jQuery 选择器的更多帮助,请访问此处。
Taking ABentSpoon's response a step further, your jQuery selector can search for all anchor tags that start with a slash.
For more help with jQuery selectors, go here.
为什么不直接使用绝对路径呢?
Why don´t you just use absolute paths?
你们确实帮助了我,非常感谢。我认为我需要的正则表达式是
/href[\s]?=[\s\"\']\./is
正如 ABentSpoon 指出的“如果它以斜线开头,那是绝对的 从其他站点读取页面是一个很好的练习。幸运的是,我可能希望执行此操作的任何页面都位于同一站点和同一服务器上。对于大多数目的来说足够了”。不过,我想启用 Jeroen 评论说,仅使用绝对路径,这并不是一个真正的选择,因为该网站上有很多页面,而且每个页面都会根据从内部或外部访问的位置而获得不同的地址(DNS)。你给你的链接一个绝对路径,你把所有的链接都绑定到那个站点的DNS名称上,当你发现这个改变太频繁时,或者就此而言,部门觉得有必要改变他们的子目录名称时,这是一个问题。希望将这个功能设计得更灵活一些。
我肯定会阅读有关 jQuery 的内容,这看起来很有趣,但我还没有玩过……更多的学习内容即将到来;-)
再次感谢你们抽出时间。
You guys have certainly helped me out here, many thanks. I think the regular expression I need would be
/href[\s]?=[\s\"\']\./is
as ABentSpoon pointed out "If it starts with a slash, that's absolute enough for most purposes". However I guess it would be a good excersise to enable reading pages from other sites. Luckily any of the pages I may wish to do this with are on a same site, and on same server.To pick up on Jeroen comment of just using absolute paths, that is not really an option as there are many pages on this site. Also each page would get addressed differently (DNS) depending on where it'll be accessed from... internally or externally. If you give your links an absolute path you tie ALL of them to having that site DNS name. A problem when you find this changing all too regularly, or for that matter depatments feel the need to change thir subdirectory names, but that's another story. I wish to design this feature to be a little more flexible.
I will certainly read up about jQuery. Looks interesing, it's not something I've played with yet... more learning coming up ;-)
Thanks again for taking the time guys.