3

I am generating a slideshow using org-reveal, and have links to other web-pages. During the presentaion (conducted from the browser), I would like clicking on these links to result in opening the link in a new browser tab.

I tried the docu, but couldn't find anything. Any suggestions?

  • Are you clicking on the link in the org buffer in emacs or in the generated slides in the web browser? – Kaushal Modi Jul 29 '15 at 10:27
  • From the browser. I fixed the ambiguity in the question. – Spacemoose Jul 29 '15 at 10:29
  • In that case, I am not sure if that's an emacs or org question, but someone can correct me. In Chrome and Firefox, I know that middle clicking a link opens it in new tab. – Kaushal Modi Jul 29 '15 at 10:31
  • That will work, but I'd like to change the behavior of a left-click, since I might screw up while in the presentation, and that would be a hassle. – Spacemoose Jul 29 '15 at 10:33
  • I haven't tried this yet, but you can modify the ox-reveal exporter to add target="_blank" to the <a ... > tag generated for links. Source: w3schools.com/tags/att_a_target.asp. Or try this out: orgmode.org/manual/… – Kaushal Modi Jul 29 '15 at 10:40
5

The below works for both html and reveal exports:

* External Links
=[[LINK][LINK NAME]]=
** Open link in the same tab
#+BEGIN_SRC text
Check out the org mode manual on [[http://orgmode.org/manual/Hyperlinks.html]
[Hyperlinks]].

/(Left clicking the link will open the link in the same tab.)/
#+END_SRC
Check out the org mode manual on [[http://orgmode.org/manual/Hyperlinks.html][Hyperlinks]].

/(Left clicking the link will open the link in the same tab.)/
** Open link in a new tab
#+BEGIN_SRC text
#+ATTR_HTML: :target _blank
Check out the org mode manual on [[http://orgmode.org/manual/Hyperlinks.html]
[Hyperlinks]] in a new tab.

/(Left clicking the link will open the link in a new tab.)/
#+END_SRC
#+ATTR_HTML: :target _blank
Check out the org mode manual on [[http://orgmode.org/manual/Hyperlinks.html][Hyperlinks]] in a new tab.

/(Left clicking the link will open the link in a new tab.)/

HTML Export

enter image description here

  • 2
    Is thee a way to apply this to the entire org-mode document? – Brian Fitzpatrick Aug 19 '15 at 6:08
2

A little JavaScript will do.

Org file:

#+HTML_HEAD: <script type="text/javascript" src="post.js"></script>

I used the Bash shell keyword ~time~ to obtain time consumed by the
building process.  I added the option ~-rtlib=compiler-rt~ to
~LDFLAGS~, since Clang would complain ~undefined reference to
`__muloti4'~ without it (see [[clang-bug:16404][bug]]).  Clang 5.0 also provides a linker
called ~lld~ and claims that it is faster than the built-in linker.
As per [[using-lld][instruction]], I added ~-fuse-ld=lld~ to ~LDFLAGS~ as a third
group.

#+LINK: clang-bug https://bugs.llvm.org/show_bug.cgi?id=%s
#+LINK: using-lld https://lld.llvm.org/#using-lld

JavaScript file named "post.js":

document.addEventListener("DOMContentLoaded", () => {
    const anchors = document.getElementsByTagName("a");
    for (const anchor of anchors) {
        anchor.target = "_blank";
    }
});

Put the JavaScript file in the same folder as the exported HTML file. Then you are fine.

If you want to support older browsers, you can use the following code instead.

window.onload = function() {
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        anchors[i].target = "_blank";
    }
};

In addition, you probably do not want to open an internal link (like this link) in a new tab. Then you can use the following code as I did on my own site.

window.onload = function() {
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.hostname !== window.location.hostname
            || anchor.pathname !== window.location.pathname) {
            anchor.target = "_blank";
        }
    }
};
0

For simply making target="_blank" the default target, you can put the following line to your Org file:

#+HTML_HEAD: <base target="_blank">
0

The Advanced-Export-Configuration guide mentions filter hooks for elements. Using this and a rule like "all links whose href starts with http" we can build a filter that adds the target attribute to matching links:

(defun my-org-export-add-target-blank-to-http-links (text backend info)
  "Add target=\"_blank\" to external links."
  (when (and
         (org-export-derived-backend-p backend 'html)
         (string-match "href=\"http[^\"]+" text)
         (not (string-match "target=\"" text)))
    (string-match "<a " text)
    (replace-match "<a target=\"_blank\" " nil nil text)))

(add-to-list 'org-export-filter-link-functions
             'my-org-export-add-target-blank-to-http-links)

If you don't want this as global behavior you can set it per-buffer with the BIND keyword:

#+BIND: org-export-filter-link-functions (my-org-export-add-target-blank-to-http-links)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.