Is it possible to get page redirect information via Ajax?

前端 未结 5 1013
眼角桃花
眼角桃花 2021-02-19 19:27

Without ajax, if we load http://example.com/1 and if it redirects to http://example.com/2 then the browser gets appropriate headers and the Browser URL

5条回答
  •  眼角桃花
    2021-02-19 19:45

    not sure if I understood it, but just tried something, if it is not what you're looking for please notify me to delete the answer.

    here we inject this /#/ in the URL so when clicking on links the browser will have a new segment in the URL which represent the parameter that depending on its value you can determine which page to load using the corresponding AJAX call..

    JS/jQuery:

    var navLinks = $('#nav a'),
        params = [],
        baseURL = '//localhost/test/js-url-parameters-second';
    
    navLinks.each(function(){
        var oldHREF = $(this).attr('href');
        $(this).attr('href', baseURL +'/#/'+ oldHREF);
    });
    
    navLinks.on('click', function(){
        checkURL($(this).attr('href'));
    });
    
    function checkURL(docURL){
        // if /#/ found then we have URL parameters
        // grabbing the parameters part of the URL
        if(docURL.indexOf('/#/') > -1){
            docURL = docURL.split('/#/')[1];
            if(docURL != ''){
                // omit the last forward slash if exists
                if(docURL[docURL.length - 1] == '/'){
                    docURL = docURL.substring(0, docURL.length - 1);
                }
                console.log(docURL);
                $('#page-type').text(docURL);
            }
        } else {
            console.log('No URL parameters found');
        }
    }
    

    HTML:

    
    
    this is the Home page

提交回复
热议问题