简体   繁体   English

选择页面加载时的语言弹出窗口

[英]Choose language popup on page load

I am building a multilingual site and I want to insert a function, on initial page load, that finds the default language setting of the browser and then prompts the user to choose which language they would like. 我正在构建一个多语言站点,我想在初始页面加载时插入一个函数,该函数查找浏览器的默认语言设置,然后提示用户选择他们想要的语言。 (Eng/Esp) (英/ ESP)

Of course the language in the prompt would be in the same language of the browser. 当然,提示中的语言将与浏览器的语言相同。 I already have navigation between these languages on all pages page but I want to intercept the user upon entrance and avoid using a splash page. 我已经在所有页面页面上的这些语言之间进行导航,但是我想在进入时拦截用户并避免使用启动页面。

Can anybody advise me on how this is done? 有人可以建议我如何做吗?

First you need a custom event listener 首先,您需要一个自定义事件监听器

function addEvent(to, type, fn) {
    // Firefox, Safari, Chrome, Opera
    if(document.addEventListener) {
        to.addEventListener(type, fn, false)
    }
    // Microsoft ActiveX Scripts
    else if(document.attachEvent) {  
        to.attachEvent('on'+type, fn)
    }
    // Last hope
    else {  
        to['on'+type] = fn
    }
}

Add event listener on the window when its loaded and run the function onDomLoaded() 加载窗口时在窗口上添加事件侦听器,并运行onDomLoaded()函数

addEvent(window, 'load', onDomLoaded)

Create the function onDomLoaded 创建函数onDomLoaded

function onDomLoaded() {
    alert('Im finished loading the entire window, your language is: ' + navigator.language)

}

Here the example on jsFiddle 这是jsFiddle上的示例

Like Deadlock has said you can use a JavaScript Modal pop-up. 就像Deadlock所说的那样,您可以使用JavaScript Modal弹出窗口。 Someone has given an example of this Here . 有人在这里给出了一个例子。

You can create this pop-up containing buttons to select the language. 您可以创建包含选择语言的按钮的弹出窗口。

Answer on how to get the browser language using JavaScript . 回答如何使用JavaScript获取浏览器语言。 And as you specified PHP in your question tags, answer to do so with PHP . 而当你在你的问题标签指定PHP,回答有这样做的PHP

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

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