根据位置更新html页面的部分内容.href客户端
Update parts of an html page based on the location.href client-side
我试过搜索,没有找到工作。网站这是不与php工作,我知道如何与php,但我需要任何其他方式。
我只是想要一个简单的IF然后声明说,如果网站是www.a.com然后"这个样式表"这个页面标题"这个标志"等。我将在页面中多次使用if函数。如果网站是。com,这个图片,这个文本等等,如果是。com,那么一切都是不同的。
我还希望它只识别域本身,所以如果在a.com/thispage.html上,那么它仍然会加载适当的数据。
原因是,我有两个网站指向相同的文件夹,这是一个商店,相同的产品等,但我们营销的产品作为"a"answers"b"。所以我只是想看看什么网站上的用户和拉适当的信息关于该网站。
这是我想出来的,但不能生成所需的html。
<script>
window.onload = function ()
var element = document.getElementbyId('idElement');
if (location.href == "a.com")
{
<link href="/landing.css" rel="stylesheet" type="text/css" />
}
if (location.href == "b.com")
{
<link href="/landing2.css" rel="stylesheet" type="text/css" />
}
</script>
<script>
if (location.href == "a.com")
{
<title>AAAAAAAAAAAAA</title>
}
if (location.href == "b.com")
{
<title>BBBBBBBBBBBBB</title>
}
</script>
<script>
if (location.href == "a.com")
{
<img src="a.png">
}
if (location.href == "b.com")
{
<img src="b.png">
}
</script>
可以通过使用一个数组来保存每个站点的元数据对象来实现这一点。当脚本运行时,您将为css创建一个新的link元素,并将其添加到头部并设置文档标题。
请注意,DOM内容只能在加载后才能找到(然后更改),因此使用了DOMContentLoaded的eventlistener。
这将导致html脚本标签中的以下实现:<html>
<head>
<title>
NotSet
</title>
<script>
(function () {
"use strict";
// have an array sites with data
var sites = [{
url: 'a.com', // if location.href contains this
title: 'AAAAAAAAAAAAA', // use this title
css: '/landing.css', // use this css file
images: [{id: 'idOfImage', src: 'a.png'}] // replace those ids with src
}, {
url: 'b.com',
title: 'BBBBBBBBBBBBB',
css: '/landing2.css',
images: [{id: 'idOfImage', src: 'b.png'}]
}
],
site, siteIndex, NOT_FOUND = -1;
//create a link for the css and add it
function addLink(css) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = css;
document.head.appendChild(link);
}
// find the img tags by id and set the src
function setImages(images) {
var image, imgIndex, img;
for (imgIndex = 0; imgIndex < images.length; imgIndex = imgIndex + 1) {
image = images[imgIndex];
img = document.getElementById(image.id);
if (img !== null) {
img.src = image.src;
}
}
}
// iterate over our sites array
// at the end site will have an object or is null (if no match found)
for (siteIndex = 0; siteIndex < sites.length; siteIndex = siteIndex + 1) {
site = sites[siteIndex];
// is url found in location.href
if (window.location.href.indexOf(site.url) > NOT_FOUND) {
break;
}
site = null;
}
// if we have a site do what is needed
if (site !== null) {
addLink(site.css);
// IE9 or up...
document.addEventListener("DOMContentLoaded",
function () {setImages(site.images); }
);
// set the title
document.title = site.title;
}
}());
</script>
</head>
<body>
Does this work?
<img id="idOfImage" src="none.png" />
</body>
<html>
相关文章:
- 客户端服务器REST API captcha实现
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何使用Socket.io将命令从客户端发送到服务器
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- 如何将我的javascript库公开给其他客户端使用
- 是否可以使用JavaScript/AAJAX在客户端创建一个文件
- 如何使用Javascript客户端对象模型检索Sharepoint 2010列表项权限
- 返回/从Twit's客户端.get
- 将客户端特定的日期格式返回到服务器MVC4
- MobileFirst:在客户端运行计时器作业-最佳选项
- 标签客户端的设置值
- 我的客户端选项是什么
- Node.js上的WebSocket,并在所有连接的客户端之间共享消息
- 使用javascript在客户端上使用Web服务
- 通过ajax从客户端调用C#方法来执行C#方法
- SignalR客户端启动连接时如何设置用户
- 根据位置更新html页面的部分内容.href客户端
- 使用 document.location.href 保存客户端数据时,如何向浏览器建议文件名