Accessing #hash parameters from url on server side

1,049 views
Skip to first unread message

Eugene Dzhurinsky

unread,
Jun 12, 2013, 5:04:34 PM6/12/13
to lif...@googlegroups.com
Hello!

Now I need to handle urls like http://zzz.com/page#param1=value&param2=value... and extract parameters from it.

How do I get access to these parameters? Or if it's not possible, how to access the string after hash symbol?

Thanks in advance!

David Pollak

unread,
Jun 12, 2013, 6:55:47 PM6/12/13
to liftweb
The stuff after the hash is not sent to the server. There's no way to access it server-side.
 

Thanks in advance!

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Telegram, Simply Beautiful CMS https://telegr.am
Lift, the simply functional web framework http://liftweb.net

Diego Medina

unread,
Jun 12, 2013, 10:32:23 PM6/12/13
to Lift
What I have done in the past is to ready the values after the # tag and then call scala code from javascript, you can call scala code from javascript using any of

SHtml.ajaxInvoke
SHtml.jsonCall
SHtml.ajaxCall

This post shows some info on ajaxInvoke


Hope that helps.

  Diego 



On Wed, Jun 12, 2013 at 5:04 PM, Eugene Dzhurinsky <jdev...@gmail.com> wrote:

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://fmpwizard.telegr.am

Andreas Joseph Krogh

unread,
Jun 13, 2013, 3:12:27 AM6/13/13
to lif...@googlegroups.com
I have a trait called TabbedSnippet (needs a better name..) which snippets which need to respond to "hashChanged"-events extend, which looks like this:
trait TabbedSnippet {
        final val pairRegex = "([^?=&]+)(=([^&]*))?".r
        final val tabContainerId = nextFuncName

        val uri = S.uri

        def hashParams: Map[String, String]
        def hashParams_=(newVal: Map[String, String])

        def setDefaultHashIfMissing(): JsCmd = {
                val hash = TabbedSnippet.hashParamsToUri(hashParams)
                JsIf(JsRaw("Object.isUndefined(window.location.hash) || window.location.hash.length == 0"),
                        SetExp(JsVar("window.location.hash"), Str("#" + hash))
                )
        }

        def handleHashChangeEvent(hashValue: String): JsCmd = {
                val fisk = pairRegex.findAllIn(hashValue)
                if (fisk.hasNext) {
                        hashParams = fisk.map(v => v.split("=")).map(pair => URLDecoder.decode(pair(0), "UTF-8") -> URLDecoder.decode(pair(1), "UTF-8")).toMap
                        processHashChanged()
                } else {
                        Noop
                }
        }

        def processHashChanged(): JsCmd

}
Here's the OnEvent and OnHashChange classes:
 
case class OnEvent(query: JsExp, event: String, func: AnonFunc) extends JsCmd {
        override val toJsCmd = (Jq(query) ~> new JsMember {
                override val toJsCmd = "on(" + event.encJs + ", " + func.toJsCmd + ")"
        }).cmd.toJsCmd
}
case class OnHashChange(func: String => JsCmd) extends JsCmd {
        override val toJsCmd = OnEvent(JsRaw("window"), "hashchange",
                AnonFunc("eventObj",
                        Call("ORIGO.Util.hashChanged", JsVar("eventObj"), AnonFunc("hashValue", SHtml.ajaxCall(JsVar("hashValue"), func))))
        ).toJsCmd
}
 
Use like this from a snippet:
".tabChangeHook" #> Script(OnHashChange(handleHashChangeEvent) & // Install the eventhandler
        OnLoad(setDefaultHashIfMissing()))
override def processHashChanged(): JsCmd = {
        hashParams.get("tab") match {
                case Some(CalendarViewTab(tab)) => processTab(tab)
                case e => Alert("Invalid tab-value: " + e)
        }
}
 
Here's the hashChanged JS-function:
hashChanged: function(eventObj, func) {
                if (Object.isString(location.hash) && location.hash.indexOf("#") != -1 && Object.isFunction(func)) {
                    func(location.hash.substr(location.hash.indexOf("#") + 1));
                }
            }
 
Hope this helps.
 
--
Andreas Joseph Krogh <and...@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 

Antonio Salazar Cardozo

unread,
Jun 14, 2013, 10:42:48 AM6/14/13
to lif...@googlegroups.com
For the record, though fallbacks are good, if you're doing same-page transitions and such, it may be best to use pushState/popState for most modern browsers (that is what, for example, github uses). I believe there are shims that provide equal functionality for older browsers.
Thanks,
Antonio

Austen Holmes

unread,
Jun 14, 2013, 2:35:29 PM6/14/13
to lif...@googlegroups.com
https://github.com/browserstate/history.js

That's what I've used.  Falls back to hashStateChanged for older browers.
Reply all
Reply to author
Forward
0 new messages