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

Get current URL #289

Closed
Alexandre-Georges opened this issue Nov 12, 2013 · 15 comments
Closed

Get current URL #289

Alexandre-Georges opened this issue Nov 12, 2013 · 15 comments

Comments

@Alexandre-Georges
Copy link

Hi,

I was wondering if it is possible to get the current (parametrized) URL.
Using : Router.current().route.name is a bit too complicated for what I would like to do.

Thanks

@lukejagodzinski
Copy link
Contributor

Router.current().path

@Alexandre-Georges
Copy link
Author

Simple enough. Thanks.

@hipertracker
Copy link

But how to pass Router.current().path to the template?

Template.navbar.currentPath =  function () { 
    return Router.current().path;
};

does not work because Router is null. It is available later. That template binding is working BEFORE the Router object is available. How to sync it?

I only found an ugly workaround like that:

Template.navbar.currentPath =  function () { 
    return Router && Router.current() && Router.current().path;
};

@rubencodes
Copy link

Hi, this Router.current().path suggestion didn't work for me on the latest meteor/iron-router; I was able to get by using Router.current().url, however this seems to alternate inconsistently between displaying a full url and only the path depending on how I arrive on the page. Is there any new, consistent method I'm not aware of? Thanks.

@swennemans
Copy link

hi @rubencodes try:

Router.current().route.getName() 

It will give you the name of route :) Hope this helps.

@radiegtya
Copy link

or you can simply using

window.location.pathname 

it will have same result with old iron router

Router.current().path

@Alexandre-Georges
Copy link
Author

@radiegtya

It depends when you use it, if you call window.location in the onAfter method, the URL you get is the one displayed in your browser. Not the currently processed route's, the browser URL is replaced when all hook functions have been executed.

However if you use Iron.Location.get().path you get the path of the current route wherever you execute it.

As @rubencodes noticed there are some inconsistencies with Router.current().path which have been spotted by the Iron team. Now I only use Iron.Location.get().path that is far more reliable.

@radiegtya
Copy link

@Alexandre-Georges

Awesome, I am looking for this solution in iron router doc, and cannot find about this

Iron.Location.get().path

So I change all of my current code to window.location.path.

Thanks for the awesome answer

@monbro
Copy link

monbro commented Dec 11, 2014

According to my answer at http://stackoverflow.com/questions/21091868/meteor-breadcrumb/27426970#27426970 you can do: Router.current().route.path(this); within a helper method for a template.

@aramk
Copy link
Contributor

aramk commented Dec 23, 2014

Iron.Location.get().path seems to be the best way, since Router.current().url gives the full URL including the server hostname - but only sometimes. I've had to use .replace(/\w+:\/\/[^\/]+/, '').

@simakwm
Copy link

simakwm commented Dec 23, 2014

What about server code? How can I get current path and params? :/

@Nullpo
Copy link

Nullpo commented Feb 15, 2015

Hi! There is any way to get the actual URL in a template? Or I have to create a custom helper for that?

@endows
Copy link

endows commented Jun 11, 2015

I have not value

Router.current().path

I had get current url this command.

Router.current().url

@dongaj
Copy link

dongaj commented May 23, 2016

Use This :
within a helper with this template Object Name
Router.current().route.path(this)

@micktaiwan
Copy link

micktaiwan commented Apr 12, 2017

As it is not consistent between localhost and my server, this is my solution:

getUrl = function() {
  let url = Router.current().url;
  if(url.slice(0, 4) !== 'http')
    url = Meteor.absoluteUrl() + url.slice(1, url.length);
  return url;
};

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