Sense current domain / path? ...any macro for it?

136 views
Skip to first unread message

Mat

unread,
Aug 18, 2015, 9:01:37 AM8/18/15
to TiddlyWiki
How can I sense "where" the current TW file is located? I.e what's in the browser url address field.

Is there a <<url>> or <<path>> macro or something similar? Seems like a rather basic issue so I'm surprised I can't recall it being discussed and, AFAICT, we have the components for it with the permalink and permaview mechanisms.

This would be useful in filters to decide if a file is local or on a server. I came to think of the question when realizing that the GettingStarted tiddler for a freshly created tiddlyspot TW5 of course(!) ought to display the tiddlyspot saving fields.

<:-)


BJ

unread,
Aug 19, 2015, 10:49:52 AM8/19/15
to TiddlyWiki
Hi mat,
here's a macro for that

/*\
title: $:/macros/buggyj/misc/location.js
type: application/javascript
module-type: macro

<<location field>>
use:
<<location "show">> to see the all the fields
then 
<<location "href">> for url

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";


exports.name = "location";

exports.params = [
	{ name: "field" }
];
/*
Run the macro
*/

exports.run = function(field) {
if (field === "show") {
	alert (JSON.stringify(window.location));
}else try {
		return window.location[field].toString();
	} catch(e){}
}


})();

cheers

bj

Mat

unread,
Aug 19, 2015, 1:59:33 PM8/19/15
to TiddlyWiki
Excellent! Thank you BJ!!!!
For anyone wanting to try it out quickly, go to http://testinglocation.tiddlyspot.com/


Can I ask, what are the differences between:
  • origin
  • host
  • hostname
  • href

Thank you BJ!

<:-)
Reply all
Reply to author
Forward
0 new messages