Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get window.location #1150

Closed
dakom opened this issue Jan 6, 2019 · 9 comments
Closed

Can't get window.location #1150

dakom opened this issue Jan 6, 2019 · 9 comments

Comments

@dakom
Copy link
Contributor

dakom commented Jan 6, 2019

Assuming a function to get the window as a Result, like this:

fn get_window () -> Result<web_sys::Window, JsValue> {
    web_sys::window().ok_or(JsValue::from_str("couldn't get window"))
}

I can successfully get some other properties of the window, like inner_width:

fn get_inner_width() -> Result<f64, JsValue> {
    let size = get_window()?.inner_width()?.as_f64().ok_or(JsValue::from_str("uhoh"))?;
    Ok(size)
}

But trying to get the location fails:

let location_origin = get_window()?.location().origin()?; 

In case it matters, the use case is something like this:

pub fn same_origin(url:&str) -> Result<bool, JsValue> {
    if url.starts_with("http://") || url.starts_with("https://") {
        let location_origin = get_window()?.location().origin()?; 
        let url = Url::new(url)?;
        Ok(url.origin() == location_origin)
    } else {
        Ok(true)
    }
}

Not sure if this is related to #780 or #659

@chinedufn
Copy link
Contributor

Try this https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Document.html#method.location

(Sorry on mobile typing is a pain)

@dakom

This comment was marked as abuse.

@alexcrichton
Copy link
Contributor

I think this may have been a bug I've forgotten to publish actually, I just published new versions of all crates though! @dakom want to update and see if it's fixed now?

@dakom

This comment was marked as abuse.

@alexcrichton
Copy link
Contributor

Ah I can't get that to build unfortunately, do you have some steps though that could help me out?

@dakom

This comment was marked as abuse.

@alexcrichton
Copy link
Contributor

Oh if it's just a question of compiling (sorry, I thought it was runtime failing), then as mentioned in the documentation you'll need to enable a few features of the web-sys crate

@dakom

This comment was marked as abuse.

@alexcrichton
Copy link
Contributor

Either way's fine, I can do so here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants