js裡寫php代碼

來源:互聯網
上載者:User
common.jsfunction goUrl(lang){...//   $_SESSION['language']=lang   這句代碼在js裡要怎麼寫?...}


回複討論(解決方案)

js和php的互動最好用ajax來完成

一般是php裡嵌js,哪裡是js裡寫php呀,要伺服器支援的。

function goUrl(lang){
...

...
}

js裡要跟PHP關聯,一般都是要用到AJAX請求,如果是在JS載入時就得到(取)session的值,可以在js 代碼塊裡寫 如果是想在載入頁面時設定session,這個跟js就沒關係了,可以直接

main.inc.phpindex.phpdisplay('index.html');?>index.html[code=php]                                        
  • 繁
  • 簡
  • New
  • 繁,繁,繁,繁,繁 common.jsfunction goUrl(lang){ var url = document.location.href; var query = window.location.search.substring(1); var urls = url.replace(window.location.hash, '').split("?"); var vars = query.split("&"); var query = ""; for (var i=0;i
    點擊 index.html “繁” 切換語言 (繁)
    我在調用 goUrl() 的同時改變 $_SESSION['language']
    所以我就想在js裡添加php代碼,如果像下面那樣又會報錯,求解決方案
    function goUrl(lang){
    ...

    ...
    }

    1、session 變數儲存於服務端,用戶端不能主動修改
    2、你在 goUrl() 中有 location.href = urls[0]+"?"+query+window.location.hash; 跳轉
    所以你應在 urls[0] 指向的程式檔案中做 session 變數設定

    1、session 變數儲存於服務端,用戶端不能主動修改
    2、你在 goUrl() 中有 location.href = urls[0]+"?"+query+window.location.hash; 跳轉
    所以你應在 urls[0] 指向的程式檔案中做 session 變數設定



    跳轉後也是到index.php ->index.html 執行的goUrl()時在原本的連結上添加 ?lang=XX 來改變語言

    function goUrl(lang){
    var url = document.location.href;
    var query = window.location.search.substring(1);
    var urls = url.replace(window.location.hash, '').split("?");
    var vars = query.split("&");
    var query = "";
    for (var i=0;i var pair = vars[i].split("=");
    if (pair[0] != "lang" && pair[0] != "") {
    query = query + pair[0] + "=" + pair[1] + "&";
    }
    }
    query = query + "lang=" + lang;
    location.href = urls[0]+"?"+query+window.location.hash;
    }
    我想建立goUrl.php 來代替上面 在php裡怎麼實現這段js的功能

    改變語言後,頁面需要重新整理,你原來的寫法並不需要改變
    你的 goUrl.php 總還是要像 index.php 那樣輸出頁面內容的。不顯得累贅嗎?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.