weixin_33708432 2018-03-23 17:29 采纳率: 0%
浏览 24

根据URL调用AJAX状态

I am loading content in an overlay with its own URL and it is running pretty good. But I have the problem that when I enter the URL in the browserbar, it links directly to the page of the url, but I want to stay (on the homepage) and the overlay with the project should fade in.

I've tried it with the window.location.href but it killed my function completely. (I am also pretty newbie when it comes to ajax stuff)

My Code

//AJAX Function to fetch project content

var url = $('.load').data('url');

function openUrlInModal(url, target){
    $.ajax({
        url: url,
        type: "GET",
        dataType: "html",
        contentType: "text/html",
        cache: false,
        success: function(url) {
            $(target).append(url).addClass('modal');
            $('.modal').fadeIn('fast');
            console.log("target:" + target);
            console.log("url:" + url);
        }
    });
}

// Adds content to the modal on click

$('.load').bind('click', function(e) {

    var target = $(this).data("target");

    //History Pushstate
    fetchedProjectUrl = $(this).attr("href");
    history.pushState(null, null, fetchedProjectUrl);

    //Call Ajax Function
    openUrlInModal($(this).attr('href'), target);

    e.preventDefault();
});


//Hitting Back Button

$(window).on("popstate", function(){

    history.pushState(null, null, "");

    $('.modal').fadeOut('fast');
    function empty(){
        $('.modal').empty();
    }
    setTimeout(empty, 300);
});

Thanks for any help in advance :)

  • 写回答

1条回答 默认 最新

  • ?Briella 2018-03-23 17:41
    关注

    I do not understand where the URL for modal come from. If you want to stop the browser to go to the URL which was tipped in the browserbar and then executed - this isn't possible due to security reasons and senseless programming in mind of a browser. In this meaning you must code an own browser, than it's ok - whatever your browser should want to do.

    If the URL is a kind of dynamic, you must have a new window reference in your modal, like an iframe, than you can execute a separate location for it.

    Otherwise only paste the response-html in your modal innerHTML

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度