*Link target = _self rather than _blank

So, does anyone know how to make the *link command act as a, well, as if target = “_blank” rather than target = “_self”, which I think is clear, but if it’s not the best way I can put it is so that it acts as: This kind of link rather than This kind of link?

That is to say <a href="http://www.choiceofgames.com/" target="_blank">This kind of link</a> rather than <a href="http://www.choiceofgames.com/" target="_self">This kind of link</a>?

1 Like

@Reaperoa these are the ones I know of _blank _parent _self _top.

@lordirishdas Yea, unfortunately *link does not include a target attribute, just arguments for the href and anchorText variables. (So I guess I’m looking to add that argument, or figure a way to sneak it into the href variable.

There isn’t a way to do it presently. If you were willing to edit ChoiceScript’s JavaScript, I guess you could add an optional argument before the URL, like this:

*link new_window http://www.choiceofgames.com This kind of link

Good to know – had been wondering that myself.

You know what, I manged to add a variable (that, as far as I can tell, does absolutely nothing), so I’m gonna call that a win, and revert. Also, Dan, thank you, that is essentially what I was asking someone to do for me, because I know more about rocket science than javascript. *headdesk* Soooo, I’m gonna go ahead and take some nitro now and just go back to my little hole of ‘at least I know html’.

At this point, having read a few articles, I don’t think I’d use it even if someone did show me how to do it, although if someone did it may at least dim the desire to flip over my desk every time I see that *link sitting at the top of my code.


This should work:

scene.js - Replace this:

// *link
// Display URL with anchor text
Scene.prototype.link = function link(data) {
    var result = /^(\\S+)\\s*(.*)/.exec(data);
    if (!result) throw new Error(this.lineMsg() + "invalid line; this line should have an URL: " + data);
    var href = result[1];
    var anchorText = trim(result[2]) || href;
    printLink(this.target, href, anchorText);
    this.prevLineEmpty = false;
    this.screenEmpty = false;

With this:

// *link
// Display URL with anchor text
Scene.prototype.link = function link(data) {
    data = data || "";
    var args = data.split(" | ");
    if (!args[0]) throw new Error(this.lineMsg() + "invalid line; this line should have an URL");
    var href = args[0];
	var link_target = args[1] || "_self";
	if (link_target != "_self" && link_target != "_top" && link_target != "_parent" && link_target != "_blank") throw new Error(this.lineMsg() + "invalid url target: " + args[1]);
	var anchorText = args[2] || href;
    printLink(this.target, href, anchorText, link_target);
    this.prevLineEmpty = false;
    this.screenEmpty = false;

And then in ui.js, replace function printLink with this version, it just adds the link_target parameter and sets the “target” to whatever value it contains.

function printLink(target, href, anchorText<b>, link_target</b>) {
  if (!target) target = document.getElementById('text');
  var link = document.createElement("a");
  link.setAttribute("href", href);
 <b>link.setAttribute("target", link_target);</b>
  target.appendChild(document.createTextNode(" "));

The following choicescript code:

*link http://www.choiceofgames.com | _blank | CoG

Will produce: CoG

*link http://www.choiceofgames.com

Will produce: http://www.choiceofgames.com

_self is the default value, and if you want to specify an anchorText you will need to put the target in; you can leave it out if you don’t.

I know the pipe’s | are a deviation from the norm but it allows you to use spaces in your anchorText - feel free to change the separator on the second line of the function.

1 Like

@CJW Thank you. You know, when I first wrote that, I was thinking of @ating your name right of the bat, ‘cause I already knew that you’d be the one to come and say “Oh, that’s easy.” Frickin’ awesome.

Just FYI, this was probably bad for my health, as it’ll lead me to do something stupid such as trying to tweak the code that I’d have no more or less knowledge of if it was in damn alienese.

@Reaperoa You’re most welcome and, in my opinion? You can’t fiddle with stuff you don’t know much about - too much - after all, how else are you supposed to learn about them?

I’d say the vast majority of scripters/coders/programmers are self-taught, I am.