简体   繁体   English

使用JavaScript进行语言重定向

[英]Language redirection with javascript

I have to build a multi language website in Business Catalyst(not my choice), my only option for the language switcher and the fore the link rel="alternate" is JS. 我必须在Business Catalyst中建立一个多语言的网站(不是我的选择),我对于语言切换器的唯一选择是rel =“ alternate”链接是JS。 After a lot of trial and error I got this to work: 经过大量的反复试验后,我得到了这个结果:

<script type="text/javascript">
document.write("<ul>"); 
document.write("<li><a href=\"http://localhost:8888/en/" + location.pathname + "\">English</a></li>");
document.write("<li><a href=\"http://localhost:8888/fr/" + location.pathname + "\">French</a></li>");
document.write("<ul>"); 
</script>

The only problem is that it is slow and probably badly written. 唯一的问题是它运行缓慢且可能写得不好。 It is there a better way to write the code? 有没有更好的方式编写代码? Maybe one that load faster and possibly using jQuery? 也许是加载速度更快并且可能使用jQuery的一种?

Thank you very much in advance for your help 预先非常感谢您的帮助

Antonio 安东尼奥

I generally recommend avoiding string based dynamic html since it tends to be hard to maintain over time. 我通常建议避免使用基于字符串的动态html,因为随着时间的推移它往往难以维护。 Instead I recommend going for some sort of template based solution. 相反,我建议您使用某种基于模板的解决方案。 There are a few options, but one popular framework is KnockoutJs 有几种选择,但一种流行的框架是KnockoutJs

http://knockoutjs.com/ http://knockoutjs.com/

1- A a starter. 1-一个启动器。 document.write will replace the whole DOM with your list, therefore, I suggest appending the list to an html element. document.write将用您的列表替换整个DOM,因此,我建议将列表附加到html元素。

2- To improve performance, try to have less function call to reduce overhead. 2-为了提高性能,请尝试减少函数调用以减少开销。 Option 1: prepare the html and write it in one string, then append that string. 选项1:准备html并将其写入一个字符串,然后附加该字符串。 Eg 例如

var lang = "<ul><li>stuff...</li><li>other stuff....</li></ul>";

Or, for readability, 或者,为了便于阅读,

lang = "<ul>";
lang = "<li><a href=\"http://localhost:8888/en/" + location.pathname + "\">English</a></li>";
lang = "<li><a href=\"http://localhost:8888/fr/" + location.pathname + "\">French</a></li>";
lang = "</ul>";

Then 然后

$("#change_lang").html(lang);//change_lang is a div <div id="change_lang"> that you have prepared to put the links inside. 

3- Maybe you can load the html from your server directly, without having JS to print it on screen for you. 3-也许您可以直接从服务器加载html, 而无需JS在屏幕上为您打印 Put it in .html page (I'm not sure about your page structure, therefore, I cannot know why this approach might not be suitable for you) but loading html directly without waiting for JS and JQuery to load will make display much faster. 将其放在.html页面中 (我不确定您的页面结构,因此,我不知道为什么这种方法可能不适合您),但是直接加载html而无需等待JS和JQuery加载将使显示速度大大提高。

<ul>
    <li><a href="http://localhost:8888/en/yoursite.html">English</a></li>
    <li><a href="http://localhost:8888/fr/yoursite.html">French</a></li>
</ul>

4- Also try to put your JS code in separate files, and then minify them to reduce size and hence loading time. 4-还尝试将您的JS代码放在单独的文件中,然后最小化它们以减小大小,从而减少加载时间。 Having JS in separate files allows the browser to cache them and eliminate the need to load them every time. 将JS放在单独的文件中可以使浏览器缓存它们,而无需每次都加载它们。 Search for PageSpeed to show you various ways to improve your site performance. 搜索PageSpeed ,向您展示提高网站性能的各种方法。

You can include the following in your html. 您可以在html中包含以下内容。

<ul id="multilang">
    <li>
        <a href="http://localhost:8888/en/">English</a>
    </li>
    <li>
        <a href="http://localhost:8888/fr/">French</a>
    </li>
</ul>

Then use jQuery to manipulate the url. 然后使用jQuery操作网址。

$(function () {
    $.each($("#multilang a"), function () {
        $(this).attr('href', $(this).attr('href') + location.pathname);
    });
});

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

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