0

I am using ExpressJs in nodejs project where view files are of .ejs extention.

In that I have written Js code -- Where on select box option change I am reloading page.

For reloading page I have written below code

window.location.href = window.location.host.replace( /[\?#].*|$/, "/equipment/"+$('#rigSelect').val());

But it's not reloading page instead calling ajax action with canceled status

It's not working in expressjs -- window.location.href should reload page but its calling ajax action.

Let me know how should I reload page?

Edit

The Html/Script code

<%- include header.ejs %>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type="text/javascript">
            $(function () {
                var rigRecords = [ rigRecords ]
                var rigRows = <%- JSON.stringify(rigRecords) %>

                var rigId = [ rigId ]
                var rigId = <%- JSON.stringify(rigId) %>

                    function populate(selector) {
                        $.each(rigRows, function (index, val) {
                            if(rigId == val.id){
                                $(selector).append('<option value="'+val.id+'" selected>'+val.name+'</option>');
                            }else{
                                $(selector).append('<option value="'+val.id+'">'+val.name+'</option>');
                            }
                        })
                    }

                    populate('#rigSelect');

                    var records = [ records ]
                    var rows = <%- JSON.stringify(records) %>

                    $.each(rows[0], function (index, val) {
                    //$.each(val, function (index1, val1) {
                            var newTr = '<tr><td>'+val.name+' (VP Id:'+val.viewpoint_id+')</td></tr>';
                            $('#tbl_equipment').find('.tblBody').append(newTr);
                    //})
                    });
            });
            function reloadPage(){
                //req.redirect('http://google.com');
                //window.location.reload();
                window.location.href = window.location.host.replace( /[\?#].*|$/, "/equipment/"+$('#rigSelect').val());

            }
</script>
mujaffars
  • 1,395
  • 2
  • 15
  • 35
  • Possible duplicate of [Refresh a page using javascript or html](http://stackoverflow.com/questions/5294842/refresh-a-page-using-javascript-or-html) – Liam Oct 19 '15 at 10:28
  • 1
    @Liam it's not really a duplicate of that question, the OP knows *how* to do the redirect it just doesn't appear to be working for them. FWIW `window.location.href` should not trigger an AJAX call, sounds to me like something else is going on here. – James Oct 19 '15 at 10:47
  • Did you think in to use `req.render` or `req.redirect`? The route will go reload the page: http://expressjs.com/api.html#res.render – BrTkCa Oct 19 '15 at 11:25
  • @LucasCosta How to get `req` object in client side? (Html view page) – mujaffars Oct 19 '15 at 11:32
  • @mujaffars sorry, `req` is used for server-side. Do you can provide the relevante parts (header, select box and js function) from the .ejs? – BrTkCa Oct 19 '15 at 11:39
  • @LucasCosta I have edited question and added Html / Script code – mujaffars Oct 19 '15 at 11:55

1 Answers1

1

I have changed

window.location.href = window.location.host.replace( /[\?#].*|$/, "/equipment/"+$('#rigSelect').val());

to

window.location.href = '/equipment/'+$('#rigSelect').val();

And it is working correctly :)

mujaffars
  • 1,395
  • 2
  • 15
  • 35