使用Javascript显示基于第一个目录的链接的图像(Show image with link based on first directory with Javascript)

编程入门 行业动态 更新时间:2024-10-20 05:23:29
使用Javascript显示基于第一个目录的链接的图像(Show image with link based on first directory with Javascript)

我需要一些帮助,

我想根据URL的第一个目录在我的页面上显示图像。

例:

在任何此URL中都将显示image1.jpg

www.mysite.com/audio/amplifiers/400wats.html www.mysite.com/audio/ www.mysite.com/audio/amplifiers/

在其中任何一个中都会显示image2.jpg

www.mysite.com/video/spots/40wats.html www.mysite.com/video/amplifiers/400wats.html www.mysite.com/video/lighting/laser.html www.mysite.com/video/laser/

目前,只有当网址只是第一个目录时,才能显示图像,内部目录或文档中没有。

这是我现在正在使用的脚本:

<script type="text/javascript"> switch (location.pathname) { case "/audio/": document.write("From Web<BR>") break case "/video/": document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') break default: document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') break } </script>

谢谢

i need some help please,

i want to show an image on my Page depending of the first directory of the URL.

Example:

In Any of this URLs will show the image1.jpg

www.mysite.com/audio/amplifiers/400wats.html www.mysite.com/audio/ www.mysite.com/audio/amplifiers/

In any of this others will show the image2.jpg

www.mysite.com/video/spots/40wats.html www.mysite.com/video/amplifiers/400wats.html www.mysite.com/video/lighting/laser.html www.mysite.com/video/laser/

At the moment i can show the image only if the url is only the first directory, bu no in the internal directory or documents.

This is the script that i'm using right now:

<script type="text/javascript"> switch (location.pathname) { case "/audio/": document.write("From Web<BR>") break case "/video/": document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') break default: document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') break } </script>

Thank you

最满意答案

尝试这个:

var dir = location.pathname.split("/")[1]; if (dir == "audio") document.write("From Web<BR>") else if (dir == "video") document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') else document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>');

这会将路径名拆分为一个目录数组,当前文件名位于数组的末尾。 第一个目录将是数组中的第一个项目,并设置为dir变量。 与SLaks提供的正则表达式方法不同(顺便说一句,这也适用于你),此方法不使用多个正则表达式(如果存在多个正则表达式会降低性能),并且此方法也可以在不太可能的事件中正常工作/ video /目录包含/ audio /子目录,而使用正则表达式测试很难实现。 与SLaks的回答类似,我用if替换了你的switch ,只是因为if语句通常更适合这种事情。

Try this:

var dir = location.pathname.split("/")[1]; if (dir == "audio") document.write("From Web<BR>") else if (dir == "video") document.write('<A HREF="slides.htm" target="_blank"><IMG SRC="/adman/banners/joinvip.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>') else document.write('<A HREF="http://www.apple.com" target="_blank"><IMG SRC="http://www.amd.com/us-en/assets/content_type/DownloadableAssets/NEW_PIB_728x90.gif" WIDTH=728 HEIGHT=90 BORDER=0></A>');

This will split the pathname into an array of directories, with the current filename at the end of the array. The first directory will be the first item in the array and is set to the dir variable. Unlike the regex method provided by SLaks (which should also work for you, by the way), this method doesn't use multiple regular expressions (which will degrade performance if there are many) and this method would also work correctly in the unlikely event that the /video/ directory contained an /audio/ sub-directory, whereas this is difficult to achieve with a regex test. Similar to SLaks' answer, I replaced your switch with an if, just because if statements are usually more suited to this kind of thing.

更多推荐

本文发布于:2023-08-01 03:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   图像   链接   目录   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!