SB Placer
About
- This is the tool to change content of html-element according given criteria.
- There is a pure JavaScript version, without any dependency from third-party libraries.
- And jQuery-plugin for jQuery lovers.
- You can use it for phones switch, titles switch, any site content switch.
Grab it
# from `npm` npm install --save sb-placer # `bower` bower install --save sb-placer
Or you can download this repo and use sb-placer.min.js
/ sb-placer.jquery.min.js
from /dist
folder.
Setup
In the <head>
:
<!-- Stand-alone --> <!-- jQuery -->
You can use it as CommonJS
/ AMD
module.
// Stand-alonevar sbPlacer = ; // jQueryvar $ = ; ;
Now we can change the things.
// Stand-alone; // jQuery;
Configure
What we have to do to make things happen:
- give the
DOM
-node to place the data - give the criteria
- exec callback right after the change (if you want to)
DOM-node
Here it is:
// Stand-alone; // jQuery;
.sb-phone
— it's a simple css-selector. You can use classes, ids and all the stuff. In this example all nodes with the class sb-phone
will be innerHTML
'ed with the given data.
This is the only difference between the stand-alone and jQuery configurations.
Criteria
// get the visitor's current source with Sourcebustervar source = sbjsgetcurrentsrc; // magic!;
default_value
— String (you can usehtml
here)conditions
— Array of Objects
Condition object
check
— String / Array of Stringswhen
— String / Array of Strings / RegExpplace
— String (you can usehtml
here)
We place
data if check === when
. In the example above if source
is 'google'
— it will place '8-800-GOOGLE-PHONE'
inside the node.
Ok, it was simple. What about this one:
;
If source
is 'google'
OR 'bing'
— place '8-800-GOOGLE-BING-PHONE'
.
We want more!
var source = sbjsgetcurrentsrc medium = sbjsgetcurrentmdm; ;
If source
is ('google'
OR 'bing'
) AND (medium
is 'organic'
) — place '8-800-GOOGLE-BING-PHONE'
.
NB! This is important: [check].length === [when].length
, or this condition will be omitted.
And finally RegExp
:
var source = sbjsgetcurrentsrc medium = sbjsgetcurrentmdm campaign = sbjsgetcurrentcmp; ;
Callback
You can exec callback right after the placement was done (it's optional). It takes 3 arguments: check
, when
& place
— values from matched object.
If nothing matched:
check = false
when = false
place = default_value
var { // bla-bla} ;
P.S.
This script interacts with DOM-nodes, so pay attention to where you put the placer. Use jQuery's $(document).ready
(or native event DOMContentLoaded
with fallback) or place the placer after the target DOM
-node.
P.P.S.
It's MIT.
Have fun!