0

I am trying to use Moovit embedded services.

I want to put an origin and destination address automatically. So, I've this function in a JS file that receives the origin address and open a new window. I don't know how to get this data from JS in HTML.

function print_moovitmap(origin) {
    origin_ad = origin; //Origin_ad is global
    window.open('/new', '_blank');


}

I am working with Django. I have also origin_ad in a form field with an Id but I couldn't access to this form field in HTML. It gives me null.

    origin_address=forms.CharField(max_length=200,widget=forms.TextInput(attrs={'class':'data_aux data','id':'origin_address'}))

In HTML (null):

console.log(document.getElementById('origin_address'));

And in new.html:

<div class="mv-wtp" id="mapa_moovit" data-to='' data-lang="es">

    <script>
        var m = document.getElementById('mapa_moovit');
        m.setAttribute("data-to", "Central Park");

        m.setAttribute("data-from", "Times Sq");
    </script>
    {% load static %}

    <script src="{% static 'moovit_script.js'%}"></script>

</div>

I need to set attribute data-from to origin_ad but I don't know how.

Thank you.

1 Answer 1

1

If you make sure origin is a window property (globally declared as a window.origin = originValue* rather than const or let) you can access it in JavaScript in the opened window as

 const origin = window.opener.origin;

provided that when you open the window you specify rel=opener in the windowfeatures string:

 window.open('/new', '_blank', "rel=opener");

See also window.opener, window.open, link types

Current browser support for accessing the opening window from a frame opened with _BLANK as the target does not seem to be explicitly provided. If you run into issues, you might try using a different target name, but repeatedly opening '/new' in a named window will always open it in the same window/tab. If this is preferable anyway, don't use _BLANK.

*Note that declaring opening window variables using var should be accessible from opened windows but feed back suggests that explicitly declaring them as window properties has wider browser support.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.