dongshanxun6479 2016-10-20 10:08
浏览 118

如何在javascript代码中传递带url的变量?

I want to pass a variable with URL in JavaScript code. Variable comes from database. I see many answer in StackOverflow. But I think, I don't find any right answer for my problem. Code are given bellow:

This code is in my index page.

 <script type="text/javascript">

          $(document).ready(function(){

            $("#btn-view").hide();

            $("#btn-add").click(function(){
                $(".content-loader").fadeOut('slow', function()
                {
                    $(".content-loader").fadeIn('slow');
                    **$(".content-loader").load('video_add.php?lang=$lang')**;
                    $("#btn-add").hide();
                    $("#btn-view").show();
                });
            });

            $("#btn-view").click(function(){

                $("body").fadeOut('slow', function()
                {
                    **$("body").load('video.php?lang=$lang');**
                    $("body").fadeIn('slow');
                    **window.location.href="video.php?lang=$lang";**
                });
            });

        });
    </script>

This code is in code.js file.

// JavaScript Document

$(document).ready(function(){

/* Data Insert Starts Here */
$(document).on('submit', '#emp-SaveForm', function() {

   $.post("video_create.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-SaveForm")[0].reset();
         });    
     });   
     return false;
});
/* Data Insert Ends Here */


/* Data Delete Starts Here */
$(".delete-link").click(function()
{
    var id = $(this).attr("id");
    var del_id = id;
    var parent = $(this).parent("td").parent("tr");
    if(confirm('Sure to Delete ID no = ' +del_id))
    {
        $.post('video_delete.php?lang=$lang', {'del_id':del_id}, function(data)
        {
            parent.fadeOut('slow');
        }); 
    }
    return false;
});
/* Data Delete Ends Here */

/* Get Edit ID  */
$(".edit-link").click(function()
{
    var id = $(this).attr("id");
    var edit_id = id;
    if(confirm('Sure to Edit ID no = ' +edit_id))
    {
        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('video_edit.php?edit_id='+edit_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });
    }
    return false;
});
/* Get Edit ID  */

/* Update Record  */
$(document).on('submit', '#emp-UpdateForm', function() {

   $.post("video_update.php?lang=$lang", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-UpdateForm")[0].reset();
             $("body").fadeOut('slow', function()
             {
                $("body").fadeOut('slow');
                window.location.href="video.php?lang=$lang";
             });                 
         });    
    });   
    return false;
});
/* Update Record  */
});

All URL pass a variable

lang=$lang

. $lang is en or bn but it comes from database table. How can i make it usable?

  • 写回答

2条回答 默认 最新

  • dragon8837 2016-10-20 11:09
    关注

    you can set javascript array on your index page like that.

    Note : Put this script before include your code.js file

    <script>
    var global_var = {
    lang: "your value",
    };
    </script>
    

    Now in your code.js file

    'video_delete.php?lang='+global_var.lang
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题